Datasheet

We mentioned earlier 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:
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
And finally, we return the data:
return dataSet;
}
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.
To find out more about the
DataAdapter's properties and methods consult the .NET Documentation.
The
OleDbDataAdapter is in the System.Web.OleDb namespace and the SqlDataAdapter is in
the
System.Web.SqlClient namespace.
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: Provides details of connecting to the database
Command: Provides details of the command to be run
DataAdapter: Manages the command, and fetchs and updates data
DataSet: Provides a store for data
DataReader: Provides 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: Contains the base data objects (such as DataSet) common to all databases.
269
Reading from Databases
57084_08.qxp 30/01/2004 8:03 PM Page 269