Professional Documents
Culture Documents
NET Components
The ADO.NET components have been designed to factor data access from data
manipulation. There are two central components of ADO.NET that accomplish this: the
DataSet, and the .NET Framework data provider, which is a set of components including
the Connection, Command, DataReader, and DataAdapter objects.
The other core element of the ADO.NET architecture is the .NET Framework data
provider, whose components are explicitly designed for data manipulation and fast,
forward-only, read-only access to data. The Connection object provides connectivity to a
data source. The Command object enables access to database commands to return data,
modify data, run stored procedures, and send or retrieve parameter information. The
DataReader provides a high-performance stream of data from the data source. Finally,
the DataAdapter provides the bridge between the DataSet object and the data source.
The DataAdapter uses Command objects to execute SQL commands at the data source
to both load the DataSet with data, and reconcile changes made to the data in the
DataSet back to the data source.
You can write .NET Framework data providers for any data source. The .NET
Framework ships with two .NET Framework data providers: the .NET Framework Data
Provider for SQL Server and the .NET Framework Data Provider for OLE DB.
The following diagram illustrates the components of ADO.NET architecture.
Populating a DataSet from a DataAdapter
Each .NET Framework data provider included with the .NET Framework has a DataAdapter
object: the .NET Framework Data Provider for OLE DB includes an OleDbDataAdapter object,
the .NET Framework Data Provider for SQL Server includes a SqlDataAdapter object, and the
.NET Framework Data Provider for ODBC includes an OdbcDataAdapter object. A
DataAdapter is used to retrieve data from a data source and populate tables within a DataSet.
The DataAdapter also resolves changes made to the DataSet back to the data source. The
DataAdapter uses the Connection object of the .NET Framework data provider to connect to a
data source and Command objects to retrieve data from and resolve changes to the data source.
The SelectCommand property of the DataAdapter is a Command object that retrieves data
from the data source. The InsertCommand, UpdateCommand, and DeleteCommand properties
of the DataAdapter are Command objects that manage updates to the data in the data source
according to modifications made to the data in the DataSet. These properties are covered in more
detail in Updating the Database with a DataAdapter and the DataSet.
The Fill method of the DataAdapter is used to populate a DataSet with the results of the
SelectCommand of the DataAdapter. Fill takes as its arguments a DataSet to be populated, and
a DataTable object, or the name of the DataTable to be filled with the rows returned from the
SelectCommand.
The Fill method uses the DataReader object implicitly to return the column names and types
used to create the tables in the DataSet, as well as the data to populate the rows of the tables in
the DataSet. Tables and columns are only created if they do not already exist; otherwise Fill uses
the existing DataSet schema. Column types are created as .NET Framework types according to
the tables in mapping .NET Data Provider Data Types to .NET Framework Data Types. Primary
keys are not created unless they exist in the data source and
DataAdapter.MissingSchemaAction is set to MissingSchemaAction.AddWithKey. If Fill
finds that a primary key exists for a table, it will overwrite data in the DataSet with data from the
data source for rows where the primary key column values match those of the row returned from
the data source. If no primary key is found, the data is appended to the tables in the DataSet. Fill
uses any TableMappings that may exist when populating the DataSet (see Setting Up DataTable
and DataColumn Mappings).
Note If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter
will not set a PrimaryKey value for the resulting DataTable. You will need to define the
PrimaryKey yourself to ensure that duplicate rows are resolved correctly
Any number of DataAdapters can be used in conjunction with a DataSet. Each DataAdapter
can be used to fill one or more DataTable objects and resolve updates back to the relevant data
source. DataRelation and Constraint objects can be added to the DataSet locally, enabling you
to relate data from multiple dissimilar data sources. For example, a DataSet can contain data
from a Microsoft SQL Server database, an IBM DB2 database exposed via OLE DB, and a data
source that streams XML. One or more DataAdapter objects can handle communication to each
data source.
Note that the code does not explicitly open and close the Connection. The Fill method implicitly
opens the Connection that the DataAdapter is using if it finds that the connection is not already
open. If Fill opened the connection, it will also close the connection when Fill is finished. This
can simplify your code when dealing with a single operation such as a Fill or an Update.
However, if you are performing multiple operations that require an open connection, you can
improve the performance of your application by explicitly calling the Open method of the
Connection, performing the operations against the data source, then calling the Close method of
the Connection. You should strive to keep connections to the data source open for a minimal
amount of time to free up the resource to be used by other client applications.
DataRow.RowState Property
Gets the current state of the row in regard to its relationship to the DataRowCollection
Members:
The DataRowVersion values are used when retrieving the value found in a DataRow using Item
or the GetChildRows of the DataRow object.
The DataRowVersion informs you what version of a DataRow exists. Versions change under the
following circumstances:
• After calling the DataRow object's BeginEdit method, if you change the value, the
Current and Proposed values become available.
• After calling the DataRow object's CancelEdit method, the Proposed value is deleted.
• After calling the DataRow object's EndEdit method, the Proposed value becomes the
Current value.
• After calling the DataRow object's AcceptChanges method, the Original value becomes
identical to the Current value.
• After calling the DataTable object's AcceptChanges method, the Original value
becomes identical to the Current value.
• After calling the DataRow object's RejectChanges, the Proposed value is discarded,
and the version becomes Current.
Members