DataAdapter
The DataAdapter
is a core component of ADO.NET that acts as a bridge between a DataSet
and a data source. It provides the functionality to retrieve data from a data source and populate a DataSet
, and to reconcile changes made to the data in the DataSet
back to the data source.
A DataAdapter
exposes four primary methods that enable these operations:
Fill()
: Retrieves data from the data source and populates aDataSet
.FillSchema()
: Retrieves database schema information and creates aDataTable
in theDataSet
without populating it with data.Update()
: Reconciles changes made to the data in theDataSet
back to the data source.SelectCommand
: A property that retrieves the SQL statement or stored procedure used to select records from the data source.
ADO.NET provides specific implementations of DataAdapter
for different data providers, such as:
SqlDataAdapter
for SQL Server.OracleDataAdapter
for Oracle databases.OleDbDataAdapter
for OLE DB data sources.OdbcDataAdapter
for ODBC data sources.
Key Concepts
Loading Data (Fill)
The Fill()
method is used to load data from a data source into a DataSet
. It executes the SelectCommand
and uses a DataTable
(or creates one if it doesn't exist) to store the retrieved rows.
using System.Data;
using System.Data.SqlClient; // Or your specific provider
// Assume connectionString and selectCommandText are defined
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "Customers"); // Fills the DataSet with data into a DataTable named "Customers"
}
Updating Data (Update)
The Update()
method is used to send changes made in a DataSet
back to the data source. This involves inserting, updating, or deleting rows in the data source based on the RowState
property of the rows in the DataSet
.
To support the Update()
operation, the DataAdapter
must have its InsertCommand
, UpdateCommand
, and DeleteCommand
properties configured appropriately. If these are not set, attempting to update will result in an exception.
// Assume adapter, dataSet, and connection are already set up and filled
// Modify data in dataSet.Tables["Customers"]...
// dataSet.Tables["Customers"].Rows[0]["ContactName"] = "New Name";
// dataSet.Tables["Customers"].Rows[0].Delete();
// Update the data source
// adapter.InsertCommand = ...;
// adapter.UpdateCommand = ...;
// adapter.DeleteCommand = ...;
int rowsAffected = adapter.Update(dataSet, "Customers");
DataAdapter Events
DataAdapter
provides several events that allow you to hook into the data retrieval and update process:
RowUpdating
: Occurs before a row is updated in the data source.RowUpdated
: Occurs after a row has been updated in the data source.Fillizando
: Occurs before theFill()
operation begins.FillLoaded
: Occurs after theFill()
operation has completed.
Core Members
FillSchema(DataSet, SchemaType)
dataSet
: TheDataSet
to populate with the schema.schemaType
: ASchemaType
enumeration value that specifies how to retrieve the schema.
- A
DataTable[]
array containing theDataTable
objects created from the schema.
- Populates a
DataSet
with schema information from the data source. This includes table names, column names, data types, and constraints.
Fill(DataSet)
dataSet
: TheDataSet
to fill with data.
- The number of rows added to or refreshed in the
DataSet
.
- Retrieves data from the data source using the
SelectCommand
and loads it into the specifiedDataSet
. If tables with the same name already exist, data is added to them. Otherwise, new tables are created.
Update(DataSet)
dataSet
: TheDataSet
containing the changes to be applied to the data source.
- The number of rows successfully updated in the data source.
- Processes rows in the
DataSet
that have been added, modified, or deleted, and applies these changes to the data source. RequiresInsertCommand
,UpdateCommand
, andDeleteCommand
to be set.
SelectCommand
- An object that represents the command used to select records from the data source.
- This property holds the command executed by the
Fill()
method. It can be aDbCommand
object (e.g.,SqlCommand
).
InsertCommand
- An object that represents the command used to insert records into the data source.
- This property holds the command executed by the
Update()
method to insert new rows.
UpdateCommand
- An object that represents the command used to update records in the data source.
- This property holds the command executed by the
Update()
method to update existing rows.
DeleteCommand
- An object that represents the command used to delete records from the data source.
- This property holds the command executed by the
Update()
method to delete rows.