DataAdapter Class (.NET Data Access)

The DataAdapter class in ADO.NET is a crucial component for bridging the gap between a data source and a DataSet. It acts as a mediator, enabling you to retrieve data from a database, update it, and then persist those changes back to the data source.

Overview

DataAdapter objects are used to populate a DataSet and to propagate changes made to the DataSet back to the data source. It performs these actions by exposing four key methods:

Key Concepts

Data Retrieval (Fill)

The primary role of a DataAdapter is to retrieve data. This is achieved by assigning a SelectCommand to the DataAdapter instance and then calling its Fill method. The Fill method executes the SelectCommand and populates a specified DataSet with the returned rows.


// Example using SqlDataAdapter
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "Customers");
            

Data Updates (Update)

When data in the DataSet is modified (rows added, updated, or deleted), the DataAdapter can be used to synchronize these changes back to the data source. This requires setting the InsertCommand, UpdateCommand, and DeleteCommand properties of the DataAdapter, which define the SQL statements or stored procedures to execute for each type of modification.


// Example of setting update commands
adapter.InsertCommand = new SqlCommand("INSERT INTO Customers (Name, Email) VALUES (@Name, @Email)", connection);
adapter.InsertCommand.Parameters.Add("@Name", SqlDbType.VarChar, 50, "Name");
adapter.InsertCommand.Parameters.Add("@Email", SqlDbType.VarChar, 100, "Email");

// ... similar setup for UpdateCommand and DeleteCommand ...

adapter.Update(dataSet, "Customers");
            

Events

DataAdapter objects expose several events that allow you to intercept and customize the data retrieval and update processes:

Common Implementations

ADO.NET provides specific implementations of the DataAdapter class for various data providers:

Important:

When working with DataAdapter and DataSet, it's important to manage connections effectively. Ensure connections are opened only when necessary and closed promptly to avoid resource leaks.

Example Scenario

Consider a scenario where you need to display customer information from a database, allow users to make changes, and then save those changes:

  1. Create a connection to the database.
  2. Instantiate the appropriate DataAdapter (e.g., SqlDataAdapter).
  3. Set the SelectCommand to retrieve the customer data.
  4. Create a DataSet.
  5. Call the DataAdapter.Fill() method to populate the DataSet.
  6. Bind the data from the DataSet to a UI control (e.g., a DataGridView).
  7. Handle user edits in the UI.
  8. Set the InsertCommand, UpdateCommand, and DeleteCommand on the DataAdapter to define how changes are applied to the database.
  9. Call the DataAdapter.Update() method to send the modified data back to the data source.

Further Reading