C# Programming: From Problem Analysis to Program Design1 Working with Databases C# Programming: From Problem Analysis to Program Design 3 rd Edition 14.

Slides:



Advertisements
Similar presentations
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

 Open the Paradise.exe file  Application displays records in the Books database  Allows the store manager to enter an author’s name (or part of a name)
Chapter 14.3 LINQ to SQL Programming in Visual Basic 2010: The Very Beginner’s Guide by Jim McKeown Databases – Part 3.
1 Web-Enabled Decision Support Systems Database Connectivity with ADO.NET Prof. Name Position (123) University Name.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
1 Web-Enabled Decision Support Systems Advance Topics in Database Connectivity Prof. Name Position (123) University Name.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Database Access Using ADO.NET
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
C# Programming: From Problem Analysis to Program Design1 Working with Databases C# Programming: From Problem Analysis to Program Design 3 rd Edition 14.
Chapter 3 Maintaining a Database
Some Basic Database Terminology
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Advanced Visual Basic th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
Chapter 13 Database Access Using ADO.NET
Neal Stublen Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ.
Connecting to Data Sources Using ADO.NET Dr. Awad Khalil Computer Science & Engineering Department AUC.
CHAPTER EIGHT Accessing Data Processing Databases.
 2006 Pearson Education, Inc. All rights reserved Database, SQL and ADO.NET.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Mauricio Featherman, Ph.D. Washington St. University
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 25 I’m Suffering from Information Overload.
Chapter 24 I’m Suffering from Information Overload (Access Databases) Clearly Visual Basic: Programming with Visual Basic nd Edition.
HNDIT Rapid Application Development
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Binding to ListBox and ComboBox Controls List and combo boxes are frequently.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
C# Programming: From Problem Analysis to Program Design1 Working with Databases C# Programming: From Problem Analysis to Program Design 4th Edition 14.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
CIS16 Application Programming with Visual Basic
Chapter 10 ADO.
Database Applications
Chapter 10 Accessing Database Files
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

C# Programming: From Problem Analysis to Program Design1 Working with Databases C# Programming: From Problem Analysis to Program Design 3 rd Edition 14

C# Programming: From Problem Analysis to Program Design2 Part II

C# Programming: From Problem Analysis to Program Design3 DataGridView Control Placeholder control for displaying data on form –To instantiate DataGridView control, drag a table from Data Sources window to form Specify how data is formatted and displayed –DataGridView – customizable table that allows you to modify columns, rows, and borders Freeze rows and columns for scrolling purposes Hide rows or columns Provide ToolTips and shortcut menus

Placing DataGridView Control C# Programming: From Problem Analysis to Program Design4 Figure DataGridView control placed on form Added benefit: DataSet, BindingNavigator, AdapterManager, TableAdapter, and BindingSource objects automatically instantiated Table dragged from Data Sources window to the form; DataGridView Control created Component Tray

C# Programming: From Problem Analysis to Program Design5 Customize the DataGridView Object Use smart tag Figure Customizing the DataGridView control

Customize the DataGridView Object ( continued ) C# Programming: From Problem Analysis to Program Design6 Figure Edit DataGridView Columns

Formatting DataGridView Cells C# Programming: From Problem Analysis to Program Design7 Figure Formatting DataGridView cells

C# Programming: From Problem Analysis to Program Design8 Customize the DataGridView Object ( continued )

C# Programming: From Problem Analysis to Program Design9 Customize the DataGridView Object ( continued ) Figure Example using Configuration Tools output

BindingNavigator Control One of the five objects added to the component tray at the bottom of the form when the table from the Data Sources pane is placed on form Provides a standardized way to move through and process the data Much functionality is automatically programmed into the tool strip –Code was also automatically generated C# Programming: From Problem Analysis to Program Design10

BindingNavigator Control ( continued ) C# Programming: From Problem Analysis to Program Design11 Figure BindingNavigator and BindingSource objects

C# Programming: From Problem Analysis to Program Design12 Adding Update Functionality Data adapter and dataset used to update data using disconnected architecture –Data adapters and/or table adapters read data from a database into a dataset Interaction between the dataset and the actual database is controlled through the methods of the data adapter or table adapter objects –To write changed data from the dataset back to the database – using SELECT, INSERT, DELETE, and UPDATE SQL statements Properties of data adapters and/or table adapters

TableAdapterManager private void studentBindingNavigatorSaveItem_Click (object sender, EventArgs e) { this.Validate( ); this.studentBindingSource.EndEdit( ); this.tableAdapterManager.UpdateAll (this.studentDataBaseDataSet.Student); } UpdateAll( ) method of a TableAdapterManager class TableAdapterManager is extremely useful when an application pulls data from two or more tables –It uses the foreign-key relationships to retrieve and save data in related data tables C# Programming: From Problem Analysis to Program Design13

C# Programming: From Problem Analysis to Program Design14 TableAdapters Data adapter on steroids TableAdapter’s Update( ) method has to have available SQL Select, Insert, Delete, and Update commands Configure TableAdapter to update data –Select the TableAdapter object in component tray to view its properties TableAdapter has SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand properties Set the SQL query for the CommandText for these properties –Use the DataSet Designer to view and modify CommandText for these properties

C# Programming: From Problem Analysis to Program Design15 DataSet Designer Create and modify data and table adapters and their queries To start the designer, double-click a dataset in Solution Explorer window or right-click the dataset in the Data Sources window Visual representation of the dataset and table adapter is presented

Dataset Designer (continued) C# Programming: From Problem Analysis to Program Design16 Figure Dataset Designer opened TableAdapter object

C# Programming: From Problem Analysis to Program Design17 Reviewing the TableAdapter's Command Properties Figure Updating the SelectCommand Clicking in the value box beside the UpdateCommand property reveals New

Query Builder Once New is selected, three new rows are added below the UpdateCommand CommandText holds the SQL statement Open the Query Builder by clicking the CommandText value box (...) C# Programming: From Problem Analysis to Program Design18

C# Programming: From Problem Analysis to Program Design19 Query Builder ( continued ) First prompted to select the table Can type the SQL statement into the SQL pane or Use the Diagram pane to select columns you want to update Grid pane in the center can be used to filter and enter parameterized expressions Results pane can be used for testing query –Located at bottom of the Query Builder

Query Builder ( continued ) C# Programming: From Problem Analysis to Program Design20 Figure Identify the Table for the Update

C# Programming: From Problem Analysis to Program Design21 Parameters –Values provided at run time Special Symbol indicates insertion point –SQL Server – is placed in front of an identifier Example DELETE FROM Student WHERE (student_ID –Access – a question mark symbol (?) is used No identifier can follow the ? symbol with Access OLE DB and ODBC Data Providers do not support named parameters

C# Programming: From Problem Analysis to Program Design22 Query Builder Figure CommandText property value for the UpdateCommand

C# Programming: From Problem Analysis to Program Design23 Adding Queries to TableAdapter Objects TableAdapters has Fill( ) and Update( ) methods to retrieve and update data in a database Other queries can be added as methods called like regular methods –This is the added benefit TableAdapters offers over DataAdapters –Use DataSet Designer to add the additional queries (methods) –Have the option of naming these methods Methods are automatically named FillBy and GetDataBy –SQL Select statement generated along with the Fill and Get methods

C# Programming: From Problem Analysis to Program Design24 Adding Queries to TableAdapter Objects ( continued ) Use DataSet Designer window to add the additional queries –Right-click TableAdapater in the DataSet Designer window –Select Add Query from the pop-up menu This displays a TableAdapter Query Configuration tool Be asked “How should the TableAdapter query access the database?” –Select Use SQL statement –TableAdapter Query Configuration tool wizard launched

C# Programming: From Problem Analysis to Program Design25 Adding Queries to TableAdapter Objects ( continued ) Figure Multiple Queries with the TableAdapter Figure Naming the new query methods

C# Programming: From Problem Analysis to Program Design26 Add a Button and Textbox for the New Queries Buttons to execute the new TableAdapter queries can be added to the navigational tool strip Click on the navigational tool strip to the right of the Save button; a new button appears –Button enables you to add additional controls Double-click button to create event-handler method private void btnRetrieve_Click( object sender, EventArgs e ) { studentTableAdapter.FillByLastName (studentDataBaseDataSet.Student, txbxLastName.Text); }

C# Programming: From Problem Analysis to Program Design27 Connecting Multiple Tables Best to select all of the tables that you will need originally when you create the dataset object –Without regenerating the dataset, several options Use Query Builder and add INNER JOIN to Select statement for the TableAdapter’s SelectCommand –Use the graphical capabilities of the tool on Diagram Pane, or you can type the SQL statement into SQL pane Use the DataSet Designer –Double-click on the dataset file »DataSet Designer opens the DataSet and TableAdapter objects graphically displayed as a single unit

C# Programming: From Problem Analysis to Program Design28 Use the DataSet Designer to Connect Multiple Tables Change the TableAdapter CommandText for the SelectCommand so that when the Fill( ) method is called, dataset is populated with results from both tables Call the TableAdapter's Fill( ) method in the page load event handler this.studentTableAdapter.Fill( this.studentDataBaseDataSet.Student );

C# Programming: From Problem Analysis to Program Design29 Use the DataSet Designer ( continued ) Figure Revise the CommandText for the SelectCommand

Modify the SelectCommand Using the Query Builder C# Programming: From Problem Analysis to Program Design30 Figure Use the Query Builder to modify the SelectCommand CommandText

C# Programming: From Problem Analysis to Program Design31 Modify the SelectCommand to Connect Multiple Tables Using the Query Builder SELECT student_ID, student_FirstName, student_LastName, major_ID, student_Phone, major_Name, major_Chair, major_Phone FROM Student INNER JOIN Department ON Student.major_ID = Department.major_ID Once the relationship is established between the tables, add columns from the second table to the data grid –Do this by selecting the data grid's smart tag in the form design mode

Updating the Windows Form C# Programming: From Problem Analysis to Program Design32 Figure Adding fields from the second table

Updating the Windows Form (continued) C# Programming: From Problem Analysis to Program Design33 Figure Data retrieved from multiple tables

C# Programming: From Problem Analysis to Program Design34 Display Data Using Details View From Data Sources window –Use pull-down menu and select Details –Drag the entire table onto the form You get Label and TextBox objects for each column in the dataset –Label is the column identifier with spaces replacing underscores »Change its Text property from the Properties window

C# Programming: From Problem Analysis to Program Design35 Display Data Using Details View ( continued ) Figure Details view

Connect Multiple Tables When displaying data from multiple tables, change the SelectCommand for the TableAdapter so data can be retrieved from both tables. C# Programming: From Problem Analysis to Program Design36 Figure Using the Query Builder to modify the SELECT query for Multiple Tables

Modifying the Data-Bound Controls Click on individual columns in the Data Sources window to change default-bound control to a ComboBox, Label, LinkLabel, or ListBox –Or customize the data-bound control If you select controls from Toolbox (as opposed to from Data Sources windows), you have to set DataSource and DisplayMember properties –DataSource -> name of the dataset table object –DisplayMember -> name the table’s column object C# Programming: From Problem Analysis to Program Design37

Modifying Connection Strings Several options –Change the XML app.config file when the connection string is saved with the application –Use the Settings page of the Property Designer to modify the project’s settings Access this from Solution Explorer window –Settings.settings file C# Programming: From Problem Analysis to Program Design38

Language-Integrated Query (LINQ) Standard query operators defined in System.Linq namespace enable select, filter, aggregate, and partition of data Used with relational data sources, XML data, and any class that implement IEnumerable interface –IEnumerable supports iteration over a collection C# Programming: From Problem Analysis to Program Design39

Query Expressions Most query expressions begin with from and end with either select or group clause –from identifies data source Can add where clause to filter or exclude items C# Programming: From Problem Analysis to Program Design40

C# Programming: From Problem Analysis to Program Design41

C# Programming: From Problem Analysis to Program Design42

Implicitly Typed Local Variables keyword var indicates type will be determined from the expression var queryResult = from aName in nameArray where aName.Length > 5 orderby aName descending select aName; C# Programming: From Problem Analysis to Program Design43

LINQ with Databases After connection made to data source, instead of embedding a SQL statement in string argument, include your query expression directly in your C# program this.memberTableAdapter.Fill( this.memberDataSet.MemberTable); var memberResults = from member in this.memberDataSet.MemberTable where member.LastName.Length > 4 orderby member.LastName select member; foreach (var aRecord in memberResults) this.lstBxResult.Items.Add(aRecord.FirstName + “ “ + aRecord.LastName); C# Programming: From Problem Analysis to Program Design44

LINQ to SQL Used to query SQL Server databases Defines a mapping framework –Mapping defines classes that correspond to tables in database Dlinq is version of LINQ that focuses on querying from relational data sources XLinq is the aspect geared toward querying XML data C# Programming: From Problem Analysis to Program Design45

Coding Standards Database tables should be designed to have a primary key Use uppercase characters for SQL keywords Avoid using spaces within database names C# Programming: From Problem Analysis to Program Design46

C# Programming: From Problem Analysis to Program Design47 Chapter Summary ActiveX Data Object (ADO.NET) classes can be used to retrieve, manipulate, and update data in databases ADO.NET Data Providers Connect to the database –Connection string Programmatically access and update database

C# Programming: From Problem Analysis to Program Design48 Chapter Summary ( continued ) Data reader class – forward read-only retrieval –Read( ) Disconnected architecture SQL statements DataAdapter and TableAdapter –Fill( ) & Update( ) methods DataSet

C# Programming: From Problem Analysis to Program Design49 Chapter Summary ( continued ) Configuration tools –Use of Server Explorer Creation of New SQL Server database –Use of Data Sources window Add connections –Use of DataSet Designer Query Builder DataGridView Control

Chapter Summary ( continued ) LINQ Query Expressions –Contextual keywords LINQ with databases LINQ to SQL Implicitly typed local variables –var C# Programming: From Problem Analysis to Program Design50