Datasheet

Dim dataAdapter As System.Data.IDbDataAdapter = _
New System.Data.OleDb.OleDbDataAdapter
We mentioned that the data adapter is the link between our page and the data. As part of this link, the
adapter provides not only data fetching, but also data modification. It does so with different command
objects, exposed as properties of the adapter. These allow the different commands to run depending
upon the action being performed. In this example, we are fetching data so we use the
SelectCommand
property (so named because we are selecting rows to view).
dataAdapter.SelectCommand = dbCommand
If you use the data adapter directly, without explicitly creating a Command, this is what it does behind
the scenes.
To fetch the data, we then create a
DataSet and use the Fill(). method of the adapter:
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
And finally, we return the data:
Return dataSet
End Function
This code is more complex than the previous example, but it follows a similar path. It creates a
connection, creates a command, creates a data adapter, and then a
DataSet. A look at these objects and
their relationships in more detail will give you a clearer picture of how they work together.
ADO.NET
All of the data access we've seen so far is based upon ADO.NET – the common name for all of the data
access classes. We'll only be looking at a few of these, and the ones you'll use most are:
Connection: to provide the details of connecting to the database
Command: to provide the details of the command to be run
DataAdapter: to manage the command, and fetch and update data
DataSet: to provide a store for the data
DataReader: to provide quick read-only access to data
ADO.NET is designed to talk to multiple databases, so there are different objects for different database
types. To keep the separation, ADO.NET classes are contained within different namespaces:
System.Data, which contains the base data objects (such as DataSet) common to all databases.
System.Data.OleDb, which contains the objects used to communicate to databases via
OLEDB. OLEDB provides a common set of features to connect to multiple databases, such as
Access, DBase, and so on.
274
Chapter 8
57076_Ch 8 SAN.qxd 01/12/2003 6:43 PM Page 274