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:

ADO.NET provides specific implementations of DataAdapter for different data providers, such as:

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:

Core Members

FillSchema(DataSet, SchemaType)

Parameters:
  • dataSet: The DataSet to populate with the schema.
  • schemaType: A SchemaType enumeration value that specifies how to retrieve the schema.
Returns:
  • A DataTable[] array containing the DataTable objects created from the schema.
Remarks:
  • Populates a DataSet with schema information from the data source. This includes table names, column names, data types, and constraints.

Fill(DataSet)

Parameters:
  • dataSet: The DataSet to fill with data.
Returns:
  • The number of rows added to or refreshed in the DataSet.
Remarks:
  • Retrieves data from the data source using the SelectCommand and loads it into the specified DataSet. If tables with the same name already exist, data is added to them. Otherwise, new tables are created.

Update(DataSet)

Parameters:
  • dataSet: The DataSet containing the changes to be applied to the data source.
Returns:
  • The number of rows successfully updated in the data source.
Remarks:
  • Processes rows in the DataSet that have been added, modified, or deleted, and applies these changes to the data source. Requires InsertCommand, UpdateCommand, and DeleteCommand to be set.

SelectCommand

Returns:
  • An object that represents the command used to select records from the data source.
Remarks:
  • This property holds the command executed by the Fill() method. It can be a DbCommand object (e.g., SqlCommand).

InsertCommand

Returns:
  • An object that represents the command used to insert records into the data source.
Remarks:
  • This property holds the command executed by the Update() method to insert new rows.

UpdateCommand

Returns:
  • An object that represents the command used to update records in the data source.
Remarks:
  • This property holds the command executed by the Update() method to update existing rows.

DeleteCommand

Returns:
  • An object that represents the command used to delete records from the data source.
Remarks:
  • This property holds the command executed by the Update() method to delete rows.

See Also