JavaScript Editor Javascript validator     Javascripts

Main Page

Previous Section Next Section

11.2 The ADO.NET Object Model

The goal of ADO.NET is to provide a bridge between your objects in ASP.NET and your back-end database. ADO.NET provides an object-oriented view into the database, encapsulating many of the database properties and relationships within ADO.NET objects. Further, and in many ways most important, the ADO.NET objects encapsulate and hide the details of database access; your objects can interact with ADO.NET objects without knowing or worrying about the details of how the data is moved to and from the database.

11.2.1 The DataSet Class

The ADO.NET object model is rich, but at its heart, is a fairly straightforward set of classes. The key class is the DataSet, which is located in the System.Data namespace.

The dataset represents a rich 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, and update the database with changes to the dataset that you've made, and update the dataset with changes in the database made by other processes.

The dataset captures not just a few rows from a single table, but represents a set of tables with all the metadata necessary to represent the relationships and constraints among the tables recorded in the original database.

The dataset is comprised of DataTable objects as well as DataRelation objects. These are accessed as the Tables and Relations properties, respectively, of the DataSet object. The most important methods and properties of the DataSet class are shown in Table 11-1.

Table 11-1. Important DataSet properties and methods

Class member


DefaultViewManager property

Gets a view of the data in the dataSet that allows filtering, searching and navigation

HasErrors property

Gets a value indicating if there are any errors in any of the rows of any of the tables

Relations property

Gets the relations collection

Tables property

Gets the tables collection

AcceptChanges method

Accepts all the changes made since loaded or since last time AcceptChanges was called (see GetChanges)

Clear method

Clears the dataset of any data

GetChanges method

Returns a copy of the dataset containing all the changes made since loaded or since AcceptChanges was called

GetXML method

Gets the XML representation of the data in the dataset

GetXMLSchema method

Gets the XSD schema for the XML representation of the data in the dataset

Merge method

Merges the data in this dataset with another dataset

ReadXML method

Reads an XML schema and data into the dataset

ReadXMLSchema method

Reads an XML schema into the dataset

RejectChanges method

Rolls back to the state since last AcceptChanges (see AcceptChanges)

WriteXML method

Writes out the XML schema and data from the dataset

WriteXMLSchema method

Writes the structure of the dataset as an XML schema

The DataRelation class contains DataRelationCollection object, which contains DataRelation objects. Each DataRelation object represents a relationship between two tables, through DataColumn objects. For example, in the Bugs database, the Bugs table is in a relationship with the People table through the PersonID column. The nature of this relationship is parent/child — for any given Bug, there will be exactly one owner, but any given person may be represented in any number of Bugs. DataTables, DataColumns, and DataRelations are explored in more detail later in this chapter. The DataTable class

The DataSet object's Tables property returns a DataTableCollection collection, which in turn contains all the DataTable objects in the dataset. For example, the following line of code creates a reference to the first DataTable in the Tables collection of a DataSet object named myDataSet.

DataTable dataTable = myDataSet.Tables[0];

The DataTable has a number of public properties, including the Columns property, which returns the ColumnsCollection object, which in turn consists of DataColumn objects. Each DataColumn object represents a column in a table.

The most important methods and properties of the DataTable class are shown in Table 11-2.

Table 11-2. Important DataTable properties and methods

Class member


ChildRelations property

Gets the collection of child relations (see Relations object)

Columns property

Gets the columns collection

Constraints property

Gets the constraints collection

DataSet property

Gets the dataset this table belongs to

DefaultView property

Gets a view of the table for filtering

ParentRelations property

Gets the Parent Relations collection

PrimaryKey property

Gets or sets an array of columns as primary key for this table

Rows property

Gets the rows collection

AcceptChanges method

Commits all the changes since last AcceptChanges

Clear method

Clears the table of all data

GetChanges method

Gets a copy of the DataTable with all the changes since last AcceptChanges (see AcceptChanges)

NewRow method

Creates a new DataRow with the same schema as the table

RejectChanges method

Rolls back changes since last AcceptChanges (see AcceptChanges)

Select method

Gets an array of DataRow objects The DataRow class

The Rows collection returns a set of rows for any given table. You 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 classic ADO may be 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 most important methods and properties of the DataRow class are shown in Table 11-3.

Table 11-3. Important DataRow properties and methods

Class member



Gets or sets the data stored in a specific column (in C# this is the indexer)


Gets or sets all the values for the row using an array


Gets the table this row is owned by


Accepts all the changes since the last time AcceptChanges was called


Gets the child rows for this row


Gets the parent row of this row


Rejects all the changes since the last time AcceptChanges was called (see AcceptChanges)

11.2.2 DBCommand and DBConnection

The DBConnection object represents a connection to a data source. This connection may be shared among different command objects.

The DBCommand object allows you to send a command (typically an SQL statement or the name of a stored procedure) to the database. Often DBCommand objects are implicitly created when you create your dataset, but you can explicitly access these objects, as you'll see in a subsequent example.

11.2.3 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.

As of this writing, ASP.NET provides two different versions of the DataAdapter object; one for use with SQL Server, and the other for use with other OLE DB providers. If you are connecting to an SQL Server database, you will increase the performance of your application by using SqlDataAdapter (from System.Data.SqlClient) along with SqlCommand and SqlConnection. If you are using another database, you will use OleDbDataAdapter (from System.Data.OleDb) along with OleDbCommand and OleDbConnection. The most important methods and properties of the DataAdapter class are shown in Table 11-4.

Table 11-4. Important DataAdapter properties and methods

Class member


AcceptChangesDuringFill property

Indicates whether or not to call AcceptChanges on a DataRow after adding it to a DataTable.

Fill method

Fills a DataTable by adding or updating rows in the dataset.

FillSchema method

Adds a DataTable object to the specified dataset. Configures the schema to the specified SchemaType.

Update method

Updates all the modified rows in the specified table of the DataSet.

11.2.4 The Data Reader

An alternative to the dataset is the DataReader object. The DataReader provides connected forward-only access to a recordset returned by executing an SQL statement or a stored procedure. DataReaders are light-weight objects ideally suited for filling a web page with data and then breaking the connection to the back-end database.

Like DataAdapter, the DataReader class comes in two flavors: SqlDataReader for use with SQL Server and OleDbDataReader for use with other databases.

The most important methods and properties of the DataReader class are shown in Table 11-5.

Table 11-5. Important DataReader properties and methods

Class member



Closes the data reader


When reading the results of a batch SQL statement, advances to the next result set (set of records)

The DataReader is a very powerful object, but you don't often use many of its methods or properties. Most of the time, you simply use the DataReader to retrieve and iterate through the records that represent the result of your query.

Note to ADO programmers: you do not issue a MoveNext command to the DataReader; by reading a record, you automatically move to the next record. This eliminates one of the most common bugs with recordsets: forgetting to move to the next record.

    Previous Section Next Section

    JavaScript Editor Javascript validator     Javascripts