Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database, SQL and ADO.NET

Similar presentations


Presentation on theme: "Database, SQL and ADO.NET"— Presentation transcript:

1 Database, SQL and ADO.NET
20 Database, SQL and ADO.NET

2 OBJECTIVES In this chapter you will learn:
The relational database model. To write basic database queries in SQL. To add data sources to projects. To use the IDE's drag-and-drop capabilities to display database tables in applications. To use the classes of namespaces System.Data and System.Data.SqlClient to manipulate databases. To use ADO.NET's disconnected object model to store data from a database in local memory. To create XML documents from data sources.

3 20.1 Introduction 20.2  Relational Databases 20.3  Relational Database Overview: Books Database 20.4  SQL Basic SELECT Query WHERE Clause ORDER BY Clause Merging Data from Multiple Tables: INNER JOIN INSERT Statement UPDATE Statement DELETE Statement 20.5  ADO.NET Object Model

4 20.6 Programming with ADO.NET: Extracting Information from a Database
Displaying a Database Table in a DataGridView How Data Binding Works 20.7 Querying the Books Database 20.8   Programming with ADO.NET: Address Book Case Study 20.9   Using a DataSet to Read and Write XML 20.10 Wrap-Up 20.11 Web Resources

5 20.1 Introduction Database Organized collection of data
Database management system (DBMS) Provides mechanisms for storing, organizing, retrieving and modifying data SQL The international standard language used universally to perform queries and to manipulate data C# programs communicate with databases and manipulate their data through ADO.NET

6 20.2 Relational Databases Relational Databases
Logical representation of data Allows the data to be accessed independently of its physical structure A relational database organizes data in tables Tables are composed of rows and columns in which values are stored Primary key A column in a table that requires a unique value that cannot be duplicated in other rows Guarantees that a primary key value can be used to uniquely identify a row Composite key A primary key that is composed of two or more columns

7 Fig. 20.1 | Employees table sample data.

8 Fig. 20.2 | Result of selecting distinct Department and Location data from the Employees table.

9 20.3 Relational Database Overview: Books Database
SQL Server database files typically end with the .mdf (“master data file”) filename extension Foreign keys Can be specified when creating a table Helps maintain the Rule of Referential Integrity Every foreign key value must appear as another table’s primary key value Allow related data in multiple tables to be selected from those tables (joining the data)

10 20.3 Relational Database Overview: Books Database (Cont.)
Entity-Relationship (ER) Diagram Shows the tables in the database and their relationships First compartment in each box contains the table’s name The names in italic font are primary keys A table’s primary key uniquely identifies each row in the table Every row must have a value in the primary key column, and the value of the key must be unique in the table Known as the Rule of Entity Integrity The lines connecting the tables represent the relationships among the tables The number on the end of these lines indicates a one-to-many relationship The line between the tables links the primary key to the matching foreign key

11 Fig. 20.3 | Authors table of the Books database.

12 Fig. 20.4 | Data from the Authors table of the Books database.

13 Fig. 20.5 | Titles table of the Books database.

14 Fig. 20.6 | Data from the Titles table of the Books database.

15 Fig. 20.7 | AuthorISBN table of the Books database.

16 Fig. 20.8 | Data from the AuthorISBN table of Books.

17 Common Programming Error 20.1
Not providing a value for every column in a primary key breaks the Rule of Entity Integrity and causes the DBMS to report an error.

18 Common Programming Error 20.2
Providing the same value for the primary key in multiple rows breaks the Rule of Entity Integrity and causes the DBMS to report an error.

19 Fig. 20.9 | Entity-relationship diagram for the Books database.

20 Common Programming Error 20.3
Providing a foreign-key value that does not appear as a primary-key value in another table breaks the Rule of Referential Integrity and causes the DBMS to report an error.

21 Fig. 20.10 | Common SQL keywords.

22 20.4.1 SELECT Query SELECT keyword
“Selects” rows and columns from one or more tables in a database The basic form of a SELECT query is: SELECT * FROM tableName (*) indicates that all the columns from the tableName table should be retrieved The rows are not guaranteed to be returned in any particular order To retrieve only specific columns from a table, replace the asterisk (*) with a comma-separated list of the column names

23 20.4.2 WHERE Clause WHERE Clause
Specify the selection criteria for the query The basic form of a query with selection criteria is: SELECT columnName1, columnName2, … FROM tableName WHERE criteria WHERE clause criteria can contain the relational operators <, >, <=, >=, = (equality), <> (inequality) and LIKE, as well as the logical operators AND, OR and NOT LIKE is used for pattern matching with wildcard characters Example: SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE 'D%'

24 Fig. 20.12 | Titles with copyright dates after 2004 from table Titles.

25 Fig. 20.11 | AuthorID and LastName data from the Authors table.

26 Fig. 20.13 | Authors from the Authors table whose last names start with D.

27 Fig. 20.14 | The only author from the Authors table whose last name contains h as the second letter.

28 20.4.3 ORDER BY Clause ORDER BY Clause
Rows in the result of a query can be sorted into ascending or descending order SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC Multiple columns can be used for sorting with an ORDER BY clause of the form ORDER BY column1 sortingOrder, column2 sortingOrder, …

29 Fig. 20.15 | Authors from table Authors in ascending order by LastName.

30 Fig. 20.16 | Authors from table Authors in descending order by LastName.

31 20.4.4 Merging Data from Multiple Tables: INNER JOIN
Normalize databases Split related data into separate tables Ensure that a database does not store redundant data Merge data from multiple tables into a single result AKA “joining the tables” Specified by an INNER JOIN operator in the query Merges rows from two tables by testing for matching values in a column that is common to the tables SELECT columnName1, columnName2, … FROM table1 INNER JOIN table2 ON table1.columnName = table2.columnName ON clause Specifies the columns from each table that are compared to determine which rows are merged

32 Fig. 20.17 | Data from Titles in descending order by Copyright and ascending order by Title.

33 Fig | Books from table Titles whose titles end with How to Program in ascending order by Title.

34 Common Programming Error 20.4
In a SQL query, failure to qualify names for columns that have the same name in two or more tables is an error.

35 20.4.5 INSERT Statement INSERT Statement Inserts a row into a table
INSERT INTO tableName ( columnName1, columnName2, …, columnNameN VALUES ( value1, value2, …, valueN ) The tableName is followed by a comma-separated list of column names in ( ) List is not required if value for every column of the table in the correct order The list of column names is followed by the SQL keyword VALUES and a comma-separated list of values in ( ) Must match up with the columns specified after the table name in both order and type Always explicitly list the columns when inserting rows If the ordering changes, using only VALUES may cause an error Not every DBMS supports identity or auto incremented columns

36 Fig | Authors and ISBNs for their books in ascending order by LastName and FirstName. (Part 1 of 2.)

37 Fig | Authors and ISBNs for their books in ascending order by LastName and FirstName. (Part 2 of 2.)

38 Common Programming Error 20.5
It is an error to specify a value for an identity column.

39 Common Programming Error 20.6
SQL uses the single-quote (') character to delimit strings. To specify a string containing a single quote (e.g., O’Malley) in a SQL statement, there must be two single quotes in the position where the single-quote character appears in the string (e.g., 'O''Malley'). The first of the two single-quote characters acts as an escape character for the second. Not escaping single-quote characters in a string that is part of a SQL statement is a syntax error.

40 20.4.6 UPDATE Statement UPDATE Statement Modifies data in a table
UPDATE tableName SET columnName1 = value1, …, columnNameN = valueN WHERE criteria The tableName is followed by keyword SET and a comma-separated list of column name-value pairs in the format columnName = value WHERE clause Optional Provides criteria that determine which rows to update

41 Fig. 20.20 | Table Authors after an INSERT operation.

42 20.4.7 DELETE Statement DELETE Statement Removes rows from a table
DELETE FROM tableName WHERE criteria WHERE clause Optional Specifies the criteria used to determine which rows to delete

43 Fig. 20.21 | Table Authors after an UPDATE operation.

44 Fig. 20.22 | Table Authors after a DELETE operation.

45 20.5 ADO.NET Object Model ADO.NET
Provides an API for accessing database systems programmatically Replace Microsoft's ActiveX Data Objects (ADO) technology System.Data is the root namespace for the ADO.NET API Class DataTable Represents a table of data Contains a collection of DataRows and DataColumns System.Data.DataSet Consists of a set of DataTables and the relationships among them Represents a cache of data System.Data.OleDb Contains classes that are designed to work with any data source

46 20.5 ADO.NET Object Model (Cont.)
System.Data.SqlClient Contains classes that are optimized to work with Microsoft SQL Server databases Class SqlConnection Represents a connection to a data source Keep tracks of the location of the data source and how it is accessed Class SqlCommand Represents a SQL command that a DBMS can execute on a database

47 20.5 ADO.NET Object Model (Cont.)
ADO.NET’s Disconnected Model DataSets that is disconnected does not need a persistent connection to work with data Connect to the data source to populate the DataSet, and disconnects immediately afterwards Access and manipulate the data stored in the DataSet If necessary, reconnect to perform an update and then disconnect Class SqlDataAdapter Connects to a SQL data source Executes SQL statements to both populate and update the a DataSet Maintains a SqlConnection object that it opens and closes as needed to perform these operations using SqlCommands

48 20.6 Programming with ADO.NET: Extracting Information from a Database
The IDE provides visual programming tools and wizards that simplify accessing data in your projects These tools establish database connections and create the ADO.NET to view and manipulate the data through GUI controls

49 20.6.1 Displaying a Database Table in a DataGridView
DataGridView component Displays a data source in a GUI Displays data organized in rows and columns Connection string Specifies the path to a database file Determine how the database is accessed Adding a data source causes the IDE to generate a class derived from System.Data.DataSet Designed specifically to store data from that data source BindingNavigator component Provides several ways for users to browse and manipulate data displayed by another GUI control on a Form

50 20.6.1 Displaying a Database Table in a DataGridView (Cont.)
Step 1: Create the Project Step 2: Add a Data Source to the Project (Fig ) Step 3: Choose the Data Source Type to Add to the Project (Fig ) Step 4: Add a New Database Connection (Fig ) Step 5: Choose the Books.mdf Data Connection (Fig ) Step 6: Save the Connection String (Fig ) Step 7: Select the Database Objects to Include in Your DataSet (Fig ) Step 8: View the Data Source in the Data Sources Window (Fig ) Step 9: View the Database in the Solution Explorer (Fig )

51 Fig. 20.23 | Adding a data source to a project.
Data Sources window Data menu Fig | Adding a data source to a project.

52 Fig. 20.24 | Choosing the data source type in the Data Source Configuration Wizard.

53 Fig. 20.25 | Adding a new data connection.

54 Fig. 20.26 | Choosing the Books.mdf data connection.

55 Fig. 20.27 | Saving the connection string to the application configuration file.

56 Fig. 20.28 | Choosing the database objects to include in the DataSet.

57 Fig. 20.29 | Viewing a data source listed in the Data Sources window.

58 Fig. 20.30 | Viewing a database listed in the Solution Explorer.

59 authorsBindingNavigator
authorsDataGridView Component tray Fig | Design view after dragging the Authors data source node to the Form.

60 Fig. 20.32 | Displaying the Authors table in a DataGridView.
Move to first row Move to next row Move to previous row Move to last row (a) Delete the current row Add a new row Save changes (b) Currently selected row Fig | Displaying the Authors table in a DataGridView.

61 20.6.2 How Data Binding Works Data Binding
Technique in which GUI controls are connected to data sources Changes made through the application to the underlying data source will automatically be reflected in the data-bound control Modifying the data in the data-bound control and saving the changes updates the underlying data source The structure of an untyped DataSet is determined at runtime Based on the results of a specific query The structure of a strongly typed DataSet is known at compile time It is created specifically to store data from a particular data source A TableAdapter is responsible for interacting with a database on disk Encapsulates a SqlDataAdapter object Method Fill Retrieves information from the database and places it in the DataSet

62 20.6.2 How Data Binding Works (Cont.)
BindingSource Identifies a data source that a program can bind to a control Serves as an intermediary between the data source and the corresponding data-bound GUI control Method EndEdit Applies changes made to data through a GUI control to the data source bound to that control

63 Fig | Data binding architecture used to display the Authors table of the Books database in a GUI.

64 Outline DisplayTable.cs (1 of 2) Ensure that the object’s associated data source is updated Write the modified version of the Authors table to the database

65 Outline DisplayTable.cs (2 of 2) Retrieve information from the database and place this information in the DataSet (a) (b)

66 20.7 Querying the Books Database
Step 1: Create the Project Step 2: Add a Data Source to the Project Step 3: Create a DataGridView to Display the Titles Table Step 4: Add Custom Queries to the TitlesTableAdapter (Fig ) Step 5: Choose How the TableAdapter Should Access the Database (Fig ) Step 6: Choose a Query Type (Fig ) Step 7: Specify a SELECT Statement for the Query (Fig ) Step 8: Build a Query with Query Builder (Fig ) Step 9: Close the Query Builder (Fig ) Step 10: Set the Names of the Autogenerated Methods That Perform the Query (Fig ) Step 11: Add an Additional Query Step 12: Add a ComboBox to the Form Step 13: Customize the Form’s Load Event Handler Step 14: Program an Event Handler for the ComboBox (Fig )

67 Fig. 20.35 | Viewing the BooksDataSet in the Dataset Designer.
TitlesTableAdapter Fig | Viewing the BooksDataSet in the Dataset Designer.

68 Fig. 20.36 | TableAdapter Query Configuration Wizard to add a query to a TableAdapter.

69 Fig. 20.37 | Choosing the type of query to be generated for the TableAdapter.

70 Fig. 20.38 | Specifying a SELECT statement for the query.

71 (a) Fig | Query Builder after adding a WHERE clause by entering a value in the Filter column. (Part 1 of 2.)

72 (b) Fig | Query Builder after adding a WHERE clause by entering a value in the Filter column. (Part 2 of 2.)

73 Fig. 20.40 | The SELECT statement created by the Query Builder.

74 Fig. 20.41 | Specifying names for the methods to be added to the TitlesTableAdapter.

75 Fig. 20.42 | Dataset Designer after adding Fill and Get methods to the TitlesTableAdapter.

76 Outline DisplayQuery Result.cs (1 of 5)

77 Outline DisplayQuery Result.cs (2 of 5)

78 Executes a SELECT query that selects all rows
Outline DisplayQuery Result.cs (3 of 5) Executes a SELECT query that selects all rows Executes a SELECT query that selects all rows in which the copyright year is 2006 Executes a query that selects all rows that have “How To Program” at the end of their titles and sorts them in ascending order by title

79 Outline (b) (a) DisplayQuery Result.cs (4 of 5)

80 Outline (c) DisplayQuery Result.cs (5 of 5)

81 20.8 Programming with ADO.NET: Address Book Case Study
Step 1: Add the Database to the Project Step 2: Indicate that the IDE Should Create a Set of Labels and TextBoxes to Display Each Row of Data (Fig ) Step 3: Drag the Addresses Data Source Node to the Form (Fig ) Step 4: Make the AddressID TextBox ReadOnly Step 5: Run the Application Step 6: Add a Query to the AddressesTableAdapter (Fig ) symbol is used to indicate a parameter that will be replaced by a value when the query is executed Step 7: Add Controls to Allow Users to Specify a Last Name to Locate (Fig ) Step 8: Program an Event Handler That Locates the User-Specified Last Name Step 9: Allow the User to Return to Browsing All Rows in the Database

82 Outline AddressBook.cs (1 of 3)

83 Executes a SELECT query that selects all rows
Outline AddressBook.cs (2 of 3) Executes a SELECT query that selects all rows in which the last name matches Executes a SELECT query that selects all rows

84 Outline AddressBook.cs (3 of 3) (b) (a) (c)

85 Fig | Selecting the control(s) to be created when dragging and dropping a data source member onto the Form.

86 Fig. 20.46 | Displaying a table on a Form using a series of Labels and TextBoxes.

87 Fig | Dataset Designer for the AddressBookDataSet after adding a query to AddressesTableAdapter.

88 Fig. 20.48 | Design view after adding controls to locate a last name in the address book.

89 Fig. 20. 49 | Viewing the DataBindings
Fig | Viewing the DataBindings.Text property of a TextBox in the Properties window.

90 20.9 Using a DataSet to Read and Write XML
ADO.NET has the ability to convert data stored in a data source to XML For exchanging data between applications in a portable format Class DataSet of namespace System.Data Create XML documents from data sources Convert data from XML into data sources Method WriteXml Writes the XML representation of the DataSet instance to a file Method ReadXml Reads the XML representation of the file into its own DataSet Method GetXml Returns a string containing the XML representation of a DataSet

91 Outline XMLWriter.cs (1 of 3)

92 Outline XMLWriter.cs (2 of 3) Set baseballDataSet’s Namespace property to specify a namespace for the DataSet Generates an XML representation of the data contained in the DataSet and then write the XML to the specified file

93 Returns a string containing the XML
Outline Returns a string containing the XML XMLWriter.cs (3 of 3)

94 Outline Declares the document’s default namespace
Players.xml Each Players element represents a record in the Players table


Download ppt "Database, SQL and ADO.NET"

Similar presentations


Ads by Google