14.2 The ADO.NET Object Model
The
ADO.NET object model is rich, but at its heart it is a fairly
straightforward set of classes. The most important of these is the
DataSet. The DataSet
represents a subset of the entire database, cached on your machine
without a continuous connection to the database.
Periodically, you'll reconnect the
DataSet to its parent database, update the
database with changes you've made to the
DataSet, and update the DataSet
with changes in the database made by other processes.
This is highly efficient, but to be effective the
DataSet must be a robust subset of the database,
capturing not just a few rows from a single table, but also a set of
tables with all the metadata necessary to represent the relationships
and constraints of the original database. This is, not surprisingly,
what ADO.NET provides.
The DataSet is composed of
DataTable objects as well as
DataRelation objects. These are accessed as
properties of the DataSet object. The
Tables property returns a
DataTableCollection,
which in turn contains all the DataTable objects.
14.2.1 DataTables and DataColumns
The DataTable can be created programmatically or
as a result of a query against the database. The
DataTable has a number of public properties,
including the Columns collection, which returns
the
DataColumnCollection object, which in turn consists of
DataColumn objects. Each
DataColumn object represents a column in a table.
14.2.2 DataRelations
In addition to the Tables collection, the
DataSet has a
Relations property, which returns a
DataRelationCollection consisting of
DataRelation objects. Each
DataRelation represents a relationship between two
tables through DataColumn objects. For example, in
the Northwind database the Customers table is in a relationship with
the Orders table through the CustomerID column.
The nature of the relationship is one-to-many, or parent-to-child.
For any given order, there will be exactly one customer, but any
given customer might be represented in any number of orders.
14.2.3 Rows
DataTable's Rows
collection returns a set of rows for any given table. Use this
collection to examine the results of queries against the database,
iterating through the rows to examine each record in turn.
Programmers experienced with ADO are often confused by the absence of
the RecordSet with its moveNext
and movePrevious commands. With ADO.NET, you do
not iterate through the DataSet; instead, access
the table you need, and then you can iterate through the
Rows collection, typically with a
foreach loop. You'll see this in
the first example in this chapter.
14.2.4 Data Adapter
The DataSet is an abstraction of a relational
database. ADO.NET uses a DataAdapter as a
bridge between the DataSet and the data source,
which is the underlying database. DataAdapter
provides the Fill( ) method to retrieve data from
the database and populate the DataSet.
14.2.5 DBCommand and DBConnection
The DBConnection object represents a connection to a data
source. This connection can be shared among different command
objects. The
DBCommand object allows you to send a command
(typically a SQL statement or a stored procedure) to the database.
Often these objects are implicitly created when you create your
DataSet, but you can explicitly access these
objects, as you'll see in a subsequent example.
14.2.6 The DataAdapter Object
Rather than tie the DataSet
object too closely to your database architecture, ADO.NET uses a
DataAdapter object to mediate between the
DataSet object and the database. This decouples
the DataSet from the database and allows a single
DataSet to represent more than one database or
other data source.
|