VBHTP2e 20-Beta
VBHTP2e 20-Beta
2
0
Database, SQL
and ADO.NET
Get your facts first, and then you can distort them
as much as you please.
—Mark Twain
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.
20.1 Introduction
20.2 Relational Databases
20.3 Relational Database Overview: Books Database
20.4 SQL
20.4.1 Basic SELECT Query
20.4.2 WHERE Clause
20.4.3 ORDER BY Clause
20.4.4 Merging Data from Multiple Tables: INNER
JOIN
20.4.5 INSERT Statement
20.4.6 UPDATE Statement
20.4.7 DELETE Statement
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
– Visual Basic programs communicate with databases and
manipulate their data through ADO.NET
Column Description
AuthorID Author’s ID number in the database. In the Books database, this integer
column is defined as an identity column, also known as an autoincremented
column—for each row inserted in the table, the AuthorID value is increased
by 1 automatically to ensure that each row has a unique AuthorID. This is
the primary key.
FirstName Author’s first name (a string).
LastName Author’s last name (a string).
1 Harvey Deitel
2 Paul Deitel
3 Andrew Goldberg
4 David Choffnes
Fig. 20.4 | Data from the Authors table of the Books database.
Column Description
Fig. 20.6 | Data from the Titles table of the Books database.
Column Description
1 0131869000 2 0131450913
1 0131525239 2 0131426443
1 0131483986 2 0131857576
1 0131857576 2 0131483986
1 0131426443 2 0131525239
1 0131450913 2 0131869000
1 0131828274 3 0131450913
2 0131828274 4 0131828274
Example:
• SELECT AuthorID, FirstName, LastName
FROM Authors
WHERE LastName LIKE 'D%'
AuthorID LastName
1 Deitel
2 Deitel
3 Goldberg
4 Choffnes
Fig. 20.11 | AuthorID and LastName data from the Authors table.
Fig. 20.12 | Titles with copyright dates after 2004 from table Titles.
1 Harvey Deitel
2 Paul Deitel
Fig. 20.13 | Authors from the Authors table whose last names start with D.
4 David Choffnes
Fig. 20.14 | The only author from the Authors table whose last name
contains h as the second letter.
• ORDER BY Clause
– Rows in the result of a query can be sorted into ascending
or descending order
4 David Choffnes
1 Harvey Deitel
2 Paul Deitel
3 Andrew Goldberg
3 Andrew Goldberg
1 Harvey Deitel
2 Paul Deitel
4 David Choffnes
Fig. 20.18 | Books from table Titles whose titles end with How to Program in
ascending order by Title.
Fig. 20.19 | Authors and ISBNs for their books in ascending order by
LastName and FirstName.
• 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
1 Harvey Deitel
2 Paul Deitel
3 Andrew Goldberg
4 David Choffnes
5 Sue Smith
• 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
1 Harvey Deitel
2 Paul Deitel
3 Andrew Goldberg
4 David Choffnes
5 Sue Jones
1 Harvey Deitel
2 Paul Deitel
3 Andrew Goldberg
4 David Choffnes
• 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
– Class SqlConnection
• Represents a connection to a data source
•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
Fig. 20.24 | Choosing the data source type in the Data Source Configuration Wizard.
Fig. 20.27 | Saving the connection string to the application configuration file.
Fig. 20.29 | Viewing a data source listed in the Data Sources window.
Fig. 20.31 | Design view after dragging the Authors data source node to the Form.
– 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
Fig. 20.37 | Choosing the type of query to be generated for the TableAdapter.
Fig. 20.42 | Dataset Designer after adding Fill and Get methods to the
TitlesTableAdapter.
FrmDisplayQuery
Result.vb
(3 of 3 )
• Step 8: Add Controls to Allow Users to Specify a Last Name to Locate (Fig. 20.48)
• Step 9: Program an Event Handler That Locates the User-Specified Last Name (Fig. 20.44)
• Step 10: Allow the User to Return to Browsing All Rows in the Database (Fig. 20.44)
FrmAddressBook
.vb
(3 of 3 )
Fig. 20.45 | Selecting the control(s) to be created when dragging and dropping
a data source member onto the Form.
Fig. 20.46 | Displaying a table on a Form using a series of Labels and TextBoxes.