DataAdapters and Commands
DataAdapters are a key component in ADO.NET for bridging the gap between a data source and a Dataset. They provide a way to retrieve data from a data source, populate a Dataset, and then reconcile changes made in the Dataset back to the data source. This is primarily achieved through the use of Command objects.
Understanding DataAdapters
A DataAdapter is essentially a bridge that manages the flow of data between a Dataset and a data source. It exposes a set of commands that can be used to:
- Select data to fill a Dataset.
- Insert new records into the data source.
- Update existing records in the data source.
- Delete records from the data source.
The most common implementations are:
SqlDataAdapterfor SQL Server.OdbcDataAdapterfor ODBC data sources.OleDbDataAdapterfor OLE DB data sources.
The Role of Commands
Within a DataAdapter, Command objects (like SqlCommand, OdbcCommand, OleDbCommand) are used to interact with the database. Each DataAdapter has four main commands:
SelectCommand: Retrieves data from the data source.InsertCommand: Inserts a new record into the data source.UpdateCommand: Updates an existing record in the data source.DeleteCommand: Deletes a record from the data source.
SelectCommand
This command is used to fetch data. When you call the Fill() method of a DataAdapter, it executes the SelectCommand and populates the specified DataTable within a Dataset with the returned rows and columns.
// Example: Filling a DataTable with data
using (SqlConnection connection = new SqlConnection("your_connection_string"))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "Customers");
// Now dataSet.Tables["Customers"] contains the data
}
InsertCommand, UpdateCommand, DeleteCommand
These commands are used for modifying data. When you call the Update() method of a DataAdapter, it examines the RowState of each row in a DataTable. Based on the state, it executes the appropriate command:
Added: ExecutesInsertCommand.Modified: ExecutesUpdateCommand.Deleted: ExecutesDeleteCommand.
These commands typically use parameters to pass values from the row being inserted, updated, or deleted to the data source.
// Example: Setting up update commands
using (SqlConnection connection = new SqlConnection("your_connection_string"))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Products", connection);
// INSERT command
adapter.InsertCommand = new SqlCommand(
"INSERT INTO Products (ProductID, ProductName, UnitPrice) VALUES (@ProductID, @ProductName, @UnitPrice)",
connection);
adapter.InsertCommand.Parameters.Add("@ProductID", SqlDbType.Int, 4, "ProductID");
adapter.InsertCommand.Parameters.Add("@ProductName", SqlDbType.VarChar, 50, "ProductName");
adapter.InsertCommand.Parameters.Add("@UnitPrice", SqlDbType.Money, 8, "UnitPrice");
// UPDATE command
adapter.UpdateCommand = new SqlCommand(
"UPDATE Products SET ProductName = @ProductName, UnitPrice = @UnitPrice WHERE ProductID = @ProductID",
connection);
adapter.UpdateCommand.Parameters.Add("@ProductName", SqlDbType.VarChar, 50, "ProductName");
adapter.UpdateCommand.Parameters.Add("@UnitPrice", SqlDbType.Money, 8, "UnitPrice");
adapter.UpdateCommand.Parameters.Add("@ProductID", SqlDbType.Int, 4, "ProductID");
// DELETE command
adapter.DeleteCommand = new SqlCommand("DELETE FROM Products WHERE ProductID = @ProductID", connection);
adapter.DeleteCommand.Parameters.Add("@ProductID", SqlDbType.Int, 4, "ProductID");
// ... fill the DataSet and then call adapter.Update(dataSet, "Products")
}
Key Concepts
DataAdapter.FillSchema(): Populates a Dataset with a table schema (column definitions) without any rows.DataAdapter.Fill(): Populates a Dataset with rows and optionally schema.DataAdapter.Update(): Persists changes from a Dataset back to the data source.DataAdapter.AcceptChangesDuringFill: A property that indicates whetherAcceptChanges()is called on rows after they are added to the Dataset. Default istrue.DataAdapter.AcceptChangesDuringUpdate: A property that indicates whetherAcceptChanges()is called on rows after they are successfully updated in the data source. Default istrue.
InsertCommand, UpdateCommand, and DeleteCommand, it's crucial to use parameters to prevent SQL injection vulnerabilities and ensure data integrity.
Conclusion
DataAdapters and Commands are fundamental to ADO.NET's data manipulation capabilities. They provide a robust mechanism for retrieving, inserting, updating, and deleting data, making it possible to work with data sources in a disconnected or connected fashion.