The ADO.NET Object Model
The goal of ADO.NET is to provide a bridge between your objects in .NET and your backend database. ADO.NET provides an object-oriented API to a relational view of your database, encapsulating many of the database properties and relationships within ADO.NET objects. More importantly, 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.
19.2.1 The DataSet Class
The ADO.NET object model is rich, but at its heart is a fairly straightforward set of classes. One very powerful class, key to the disconnected architecture, is the DataSet, which is located in the System.Data namespace.
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.
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 as recorded in the original database.
The DataSet offers two key properties: Tables and Relations. The Tables property returns a collection of DataTables. Each DataTable, in turn, has two important properties: Columns and Rows. The Columns property returns a collection of DataColumn objects, while the Rows property returns a collection of DataRows.
Similarly, the Relations property of the DataSet returns a collection of DataRelation objects. The principal objects available through the DataSet are represented schematically in Figure 19-6.
Figure 19-6. The DataSet objects
Table 19-1 shows the most important methods and properties of the DataSet class.
Class Member |
Description |
---|---|
DefaultViewManager property |
Gets a view manager that provides a view of the data in the DataSet that allows filtering, searching, and navigation. |
HasErrors property |
Gets a value indicating whether there are any errors in any rows of any of 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 to the DataSet since it was 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 the last AcceptChanges (see AcceptChanges). |
WriteXML method |
Writes out the XML schema and data from the DataSet. |
WrixteXMLSchema method |
Writes the structure of the DataSet as an XML schema. |
19.2.1.1 The DataTable class
The DataSet object's Tables property returns a DataTableCollection collection, which contains tables in the DataSet. For example, the following line of code (in C#) creates a reference to the first DataTable in the Tables collection of a DataSet object named myDataSet:
DataTable dataTable = myDataSet.Tables[0];
dim dataTable as DataTable = myDataSet.Tables(0)
The DataTable has several public properties, including the Columns property, which returns the ColumnsCollection object, which consists of DataColumn objects. Each DataColumn object represents a column in a table.
The Relations 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 Bug table is in a relationship with the People table through the PersonID column. The nature of this relationship is many to onefor any given Bug, there will be exactly one owner, but any given person may be represented in any number of Bugs. The Bugs and BugHistory collection actually establish an even tighter relationship: that of parent/child. The Bug acts as a parent record for all of its history records (that is, for all the history records with the same BugID as the Bug).
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 19-2.
Class Member |
Type |
Description |
---|---|---|
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 to which this table belongs. |
DefaultView |
Property |
Gets a view of the table for filtering. |
ParentRelations |
Property |
Gets the ParentRelations collection. |
PrimaryKey |
Property |
Gets or sets an array of columns as the primary key for this table. |
Rows |
Property |
Gets the Rows collection. |
AcceptChanges |
Method |
Commits all the changes since the last AcceptChanges. |
Clear |
Method |
Clears the table of all data. |
GetChanges |
Method |
Gets a copy of the DataTable with all the changes since the last AcceptChanges (see AcceptChanges). |
NewRow |
Method |
Create a new DataRow with the same schema as the table. |
RejectChanges |
Method |
Rolls back changes since the last AcceptChanges (see AcceptChanges). |
Select |
Method |
Gets an array of DataRow objects. |
19.2.1.2 The DataRow class
The Rows collection contains DataRow objects, one for each row in the 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 process in Example 19-2.
The most important methods and properties of the DataRow class are shown in Table 19-3.
Class Member |
Type |
Description |
---|---|---|
Item |
Property |
Get or set the data stored in a specific column (in C#, this is the indexer). |
ItemArray |
Property |
Get or set all the values for the row using an array, |
Table |
Property |
Get the table that owns this row. |
AcceptChanges |
Method |
Accept all the changes since the last time AcceptChanges was called. |
GetChildRows |
Method |
Get the child rows for this row. |
GetParentRow |
Method |
Get the parent row of this row. |
RejectChanges |
Method |
Reject all the changes since the last time AcceptChanges was called (see AcceptChanges). |
19.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 a SQL statement or the name of a stored procedure) to the database. Often these objects are created implicitly when you create your DataSet, but you can explicitly access these objects, as you'll see in Example 19-4 and Example 19-5.
19.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.
.NET provides versions of the DataAdapter object; one for each data provider (e.g., SQL Server). If you are connecting to a 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 often 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 19-4.
Class Member |
Description |
---|---|
AcceptChangesDuringFill property |
Indicates whether 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. |