11.2 The ADO.NET Object ModelThe 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 ClassThe 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.
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. 11.2.1.1 The DataTable classThe 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.
11.2.1.2 The DataRow classThe 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.
11.2.2 DBCommand and DBConnectionThe 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 ObjectRather 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.
11.2.4 The Data ReaderAn 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.
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. |