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 aDataTablein theDataSetwithout populating it with data.Update(): Reconciles changes made to the data in theDataSetback 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:
SqlDataAdapterfor SQL Server.OracleDataAdapterfor Oracle databases.OleDbDataAdapterfor OLE DB data sources.OdbcDataAdapterfor 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: TheDataSetto populate with the schema.schemaType: ASchemaTypeenumeration value that specifies how to retrieve the schema.
- A
DataTable[]array containing theDataTableobjects created from the schema.
- Populates a
DataSetwith schema information from the data source. This includes table names, column names, data types, and constraints.
Fill(DataSet)
dataSet: TheDataSetto fill with data.
- The number of rows added to or refreshed in the
DataSet.
- Retrieves data from the data source using the
SelectCommandand 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: TheDataSetcontaining the changes to be applied to the data source.
- The number of rows successfully updated in the data source.
- Processes rows in the
DataSetthat have been added, modified, or deleted, and applies these changes to the data source. RequiresInsertCommand,UpdateCommand, andDeleteCommandto 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 aDbCommandobject (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.