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 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
DataTables
property returns a
TablesCollection
,
which in turn contains all the DataTable
objects.
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 ColumnsCollection
object, which in turn
consists of DataColumn
objects. Each
DataColumn
object represents a column in a table.
In addition to the DataTables
collection, the
DataSet
has a Relations
property, which returns a RelationsCollection
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.
The
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, you 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.
The DataSet
is an abstraction of a relational
database. ADO.NET uses a
Data Adapter
as a
bridge between the DataSet
and the data source:
the underlying database. Data Adapter
provides the
Fill()
method to retrieve data from the database
and populate the Data Set
.
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.
18.221.89.18