MSDN Documentation

DataTable Adapters in ADO.NET

ADO.NET's DataTable Adapters, specifically the DataAdapter class and its derived classes like SqlDataAdapter and OleDbDataAdapter, are crucial components for bridging the gap between a data source and a DataSet or DataTable. They abstract the details of retrieving data from and writing data back to a data provider.

Core Functionality

DataTable Adapters serve two primary functions:

Key Components of a DataAdapter

A typical DataTable Adapter has the following key components:

Using a SqlDataAdapter (Example)

Here's a simple example of how to use SqlDataAdapter to retrieve data and populate a DataTable:

            
using System;
using System.Data;
using System.Data.SqlClient;

public class DataAdapterExample
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=myServerAddress;Database=myDatabase;User ID=myUsername;Password=myPassword;";
        string queryString = "SELECT CustomerID, CompanyName FROM dbo.Customers;";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(queryString, connection);

            DataTable customersTable = new DataTable("Customers");

            try
            {
                connection.Open();
                adapter.Fill(customersTable);

                // Process the DataTable
                foreach (DataRow row in customersTable.Rows)
                {
                    Console.WriteLine($"ID: {row["CustomerID"]}, Name: {row["CompanyName"]}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}
            
        

Updating the Data Source

To update the data source, you first need to configure the InsertCommand, UpdateCommand, and DeleteCommand properties of the DataAdapter. ADO.NET can automatically generate these commands for SQL Server using the SqlCommandBuilder class.

            
// Assuming 'adapter' and 'customersTable' are already set up from the previous example
using (SqlCommandBuilder builder = new SqlCommandBuilder(adapter))
{
    try
    {
        // Make changes to customersTable
        // For example: customersTable.Rows[0]["CompanyName"] = "Updated Company Name";
        // customersTable.Rows.Add(new object[] { "NEWID", "New Company" });
        // customersTable.Rows[1].Delete();

        int rowsAffected = adapter.Update(customersTable);
        Console.WriteLine($"Rows updated: {rowsAffected}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error updating: {ex.Message}");
    }
}
            
        
Note: When using SqlCommandBuilder, it's essential that the SelectCommand returns at least one primary key or unique column to allow for accurate updates and deletes.

DataTable vs. DataSet

While DataAdapter is often used with DataSet, it can also directly fill and update a single DataTable. A DataSet is a collection of one or more DataTable objects, allowing you to represent complex relational data.

Provider-Specific DataAdapters

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

Tip: For optimal performance, it's recommended to use the provider-specific data adapter for your target data source.

DataTable Adapters are a fundamental part of ADO.NET for managing disconnected data. They offer a robust mechanism for fetching and persisting data, simplifying data access in .NET applications.