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.
The ADO 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 properties 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 |
Description |
---|---|
Gets a view of the data in the dataSet that allows filtering, searching and navigation | |
Gets a value indicating if there are any errors in any of the rows of any of the tables | |
Gets the relations collection | |
Gets the tables collection | |
Accepts all the changes made since loaded or since last time AcceptChanges was called (see GetChanges) | |
Clears the dataset of any data | |
Returns a copy of the dataset containing all the changes made since loaded or since AcceptChanges was called | |
Gets the XML representation of the data in the dataset | |
Gets the XSD schema for the XML representation of the data in the dataset | |
Merges the data in this dataset with another dataset | |
Reads an XML schema and data into the dataset | |
Reads an XML schema into the dataset | |
Rolls back to the state since last AcceptChanges (see AcceptChanges) | |
Writes out the XML schema and data from the dataset | |
Writes the structure of the dataset as an XML schema |
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 DataRelation property returns a 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 most important methods and properties of the DataTable class are shown in Table 11-2.
Table 11-2. Important DataTable properties amd methods
Class member |
Description |
---|---|
Gets the collection of child relations (see Relations object) | |
Gets the columns collection | |
Gets the constraints collection | |
Gets the dataset this table belongs to | |
Gets a view of the table for filtering | |
Gets the Parent Relations collection | |
Gets or sets an array of columns as primary key for this table | |
Gets the rows collection | |
Commits all the changes since last AcceptChanges | |
Clears the table of all data | |
Gets a copy of the DataTable with all the changes since last AcceptChanges (see AcceptChanges) | |
Creates a new DataRow with the same schema as the table | |
Rolls back changes since last AcceptChanges (see AcceptChanges) | |
Gets an array of DataRow objects |
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
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.
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 |
Description |
---|---|
Indicates whether or not to call AcceptChanges on a DataRow after adding it to a DataTable. | |
Fills a DataDatable by adding or updating rows in the dataset. | |
Adds a DataTable object to the specified dataset. Configures the schema to the specified SchemaType. | |
Updates all the modified rows in the specified table of the DataSet. |
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.
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.
3.12.166.255