Title: Microsoft Visual Basic 2005: Reloaded Second Edition
1Microsoft Visual Basic 2005 Reloaded Second
Edition
- Chapter 11
- Using ADO.NET 2.0
2Objectives
- After studying this chapter, you should be able
to - Define the terms used when talking about
databases - Connect a database to an application
- Bind table and field objects to controls
- Explain the purpose of the DataSet,
BindingSource, TableAdapter, and BindingNavigator
objects - Access the records in a dataset
3Objectives (continued)
- Write SQL SELECT statements
- Create a query using the Query Configuration
Wizard - Associate a ToolStrip control with a query
4Database Terminology
- Database an organized collection of related
information stored in a file on a disk - Relational database a database that stores
information in tables composed of columns and
rows - Field a single item of information
- Record a group of related fields
- Table a group of related records
- Primary key a field that uniquely identifies
each record
5Database Terminology (continued)
6Database Terminology (continued)
7Database Terminology (continued)
- Parent table contains a primary key
- Child table contains a foreign key from the
parent table to link the tables - Foreign key a field in a table that contains the
primary key of another table - Relational database advantages
- Less redundancy
- Fast retrieval
- Ability to selectively retrieve data
8ADO.NET 2.0
- ADO.NET 2.0 a technology to connect an
application to a database - ADO ActiveX Data Objects
- Dataset a copy of fields and records that the
application wants to access - Connection to the database is temporary
- Only connected when retrieving data or making
changes to data
9ADO.NET 2.0 (continued)
10Connecting a Database to an Application
11Connecting a Database to an Application
(continued)
12Connecting a Database to an Application
(continued)
13Previewing the Data Contained in a Dataset
14Previewing the Data Contained in a Dataset
(continued)
15Binding the Objects in a Dataset
- Binding connecting an object in a dataset to a
control - Bound controls controls that are connected to an
object in a dataset - Can bind an object to
- An existing control in the interface
- A control the computer creates for you
16Binding the Objects in a Dataset (continued)
17Having the Computer Create a Bound Control
- To allow the computer to create a bound control
- Drag the object from the dataset to the form
- DataGridView control displays table data in a
row and columnar format - Use the list arrow next to an objects name to
change the type of control to be created
18Having the Computer Create a Bound Control
(continued)
19Having the Computer Create a Bound Control
(continued)
20Having the Computer Create a Bound Control
(continued)
- DataGridView control
- AutoSizeColumnsMode property select Fill to
automatically adjust column widths to exactly
fill the display area - BindingNavigator control allows you to move from
one record to the next in a dataset - Four objects are placed in the component tray
- DataSet
- BindingSource
- TableAdapter
- BindingNavigator
21Having the Computer Create a Bound Control
(continued)
22Having the Computer Create a Bound Control
(continued)
- TableAdapter object connects the database to the
DataSet object - DataSet object stores the information to be
accessed from the database - BindingSource object connects the DataSet object
to the bound controls on the form
23Having the Computer Create a Bound Control
(continued)
24Having the Computer Create a Bound Control
(continued)
25Having the Computer Create a Bound Control
(continued)
- Two event procedures are automatically created in
Code Editor window - bindingNavigatorSaveItem_Click
- MainForm_Load
- bindingNavigatorSaveItem_Click event
- Saves any changes made to the dataset
- EndEdit method applies pending changes
- Update method commits the changes to the database
26Having the Computer Create a Bound Control
(continued)
- MainForm_Load event
- Fill method TableAdapter objects method to
retrieve data from the database and store it in
the dataset - DataGridView control allows data to be edited
directly within the control to update the database
27Having the Computer Create a Bound Control
(continued)
28Having the Computer Create a Bound Control
(continued)
- BindingNavigator control
- Allows movement to first, last, next, or previous
record - Allows direct selection of record by number
- Allows you to add or delete a record
- Allows you to save changes made to the dataset
29Having the Computer Create a Bound Control
(continued)
30Binding to an Existing Control
- Two ways to bind to an existing control
- Drag an object in the dataset to a control on the
form - Select the control and set properties
- Properties to bind the control are specific to
the control - DataSet, BindingSource, and TableAdapter objects
are added to the component tray - BindingNavigator control is NOT added
automatically
31Binding to an Existing Control (continued)
32Binding to an Existing Control (continued)
33Binding to an Existing Control (continued)
34Accessing the Records in a Dataset
- BindingSource properties
- Position stores position of current record
position number is zero-relative - Move moves the record pointers position
35Accessing the Records in a Dataset (continued)
36Accessing the Records in a Dataset (continued)
37Accessing the Records in a Dataset (continued)
38Accessing the Records in a Dataset (continued)
39DataSet Designer
- DataSet Designer
- Indicates the order in which to display the data
- Specifies fields and records to be viewed
40DataSet Designer (continued)
41DataSet Designer (continued)
42DataSet Designer (continued)
- Extensible Markup Language (XML) a text-based
language used to store and share data between
applications and across networks - XML schema definition file
- Defines the tables and fields that make up the
dataset - Has extension of .xsd
- Query
- Specifies the fields and records to retrieve from
the database - Specifies the field order
43DataSet Designer (continued)
44DataSet Designer (continued)
- Structured Query Language (SQL) a set of
commands to access and manipulate database data - SELECT statement
- Used to specify the fields and records to
retrieve, as well as the order of display - WHERE clause limit the records to be selected
- ORDER BY clause control the order of display
45DataSet Designer (continued)
46Creating a New Query
47Creating a New Query (continued)
48Creating a New Query (continued)
49Creating a New Query (continued)
50Creating a New Query (continued)
51Using the Query Builder Dialog Box
52Allowing the User to Run a Query
53Allowing the User to Run a Query (continued)
54Allowing the User to Run a Query (continued)
55Allowing the User to Run a Query (continued)
56Programming Tutorial
57Programming Example
58Summary
- Database an organized collection of related
information stored in a file on a disk - Relational database a database that stores
information in tables composed of columns and
rows - Primary key a field in a database table that
uniquely identifies each record - ADO.NET 2.0 a technology to access data in a
database
59Summary (continued)
- You must connect the application to a database to
create a dataset - Display dataset information by binding controls
to dataset objects - TableAdapter connects a database to a DataSet
object - BindingSource object connects a DataSet object
to bound controls on a form - BindingSource objects Position property stores
the location of the record pointer in a dataset
60Summary (continued)
- BindingSource objects Move methods used to move
the record pointer in a dataset - SQL SELECT statement specifies the fields and
records to include in a dataset - Query Configuration Wizard creates queries
- Query Builder dialog box provides a convenient
way to create a SELECT statement - Associate a query with a ToolStrip control on a
form