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.

Table 19-1. The most important DataSet properties and methods

Class Member


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

Table 19-2. The most important DataTable properties and methods

Class Member





Gets the collection of child relations (see Relations object).



Gets the Columns collection.



Gets the Constraints collection.



Gets the DataSet to which this table belongs.



Gets a view of the table for filtering.



Gets the ParentRelations collection.



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



Gets the Rows collection.



Commits all the changes since the last AcceptChanges.



Clears the table of all data.



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



Create a new DataRow with the same schema as the table.



Rolls back changes since the last AcceptChanges (see AcceptChanges).



Gets an array of DataRow objects. 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.

Table 19-3. The most important DataRow properties and methods

Class Member





Get or set the data stored in a specific column (in C#, this is the indexer).



Get or set all the values for the row using an array,



Get the table that owns this row.



Accept all the changes since the last time AcceptChanges was called.



Get the child rows for this row.



Get the parent row of this row.



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.

As of this writing, there are also ODBC and Oracle providers, with more providers being written all the time. Check with your database provider to see whether there is an ADO.NET provider for your technology.

The most important methods and properties of the DataAdapter class are shown in Table 19-4.

Table 19-4. The most important DataAdapter properties and methods

Class Member


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.

Windows Forms and the .NET Framework

Getting Started

Visual Studio .NET


Windows Forms

Dialog Boxes

Controls: The Base Class

Mouse Interaction

Text and Fonts

Drawing and GDI+

Labels and Buttons

Text Controls

Other Basic Controls

TreeView and ListView

List Controls

Date and Time Controls

Custom Controls

Menus and Bars


Updating ADO.NET

Exceptions and Debugging

Configuration and Deployment

show all menu

Programming. NET Windows Applications
Programming .Net Windows Applications
ISBN: 0596003218
EAN: 2147483647
Year: 2003
Pages: 148
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: