MSDN Documentation

DataAdapters and DataSets

ADO.NET provides a rich set of components for working with data from a data source. Among the most fundamental are DataAdapter and DataSet. These two objects work in tandem to enable disconnected data access, a powerful pattern for building responsive and scalable applications.

Understanding DataSets

A DataSet is an in-memory representation of data. It is a collection of DataTable objects, which in turn contain DataRow objects and DataColumn objects. Think of a DataSet as a workbook with multiple worksheets (DataTables), where each worksheet holds tabular data.

DataSet objects are particularly useful for scenarios where you need to:

Understanding DataAdapters

While a DataSet holds data, a DataAdapter is the bridge between the DataSet and the data source. Its primary role is to fill a DataSet with data and to propagate changes made in the DataSet back to the data source.

DataAdapter objects abstract the details of data retrieval and modification. Different providers in ADO.NET offer specific implementations of DataAdapter, such as:

A DataAdapter typically has four core commands (which are themselves IDbCommand objects):

The Disconnected Data Access Model

The combination of DataSet and DataAdapter facilitates the disconnected data access model. This model involves the following steps:

  1. Connect and Fill: Open a connection to the data source, use a DataAdapter's Fill() method to populate a DataSet.
  2. Disconnect: Close the connection to the data source. The application now works solely with the in-memory DataSet.
  3. Manipulate Data: Perform operations on the data within the DataSet (e.g., add, edit, delete rows).
  4. Reconnect and Update: Reopen the connection, and use the DataAdapter's Update() method to synchronize the changes in the DataSet back to the data source.
Note: The Update() method of a DataAdapter automatically handles the generation of appropriate SQL `INSERT`, `UPDATE`, and `DELETE` statements based on the row state (`Added`, `Modified`, `Deleted`) within the DataTable.

Example: Using SqlDataAdapter and DataSet

Here's a simplified C# example demonstrating how to use SqlDataAdapter and DataSet to retrieve and display data from a SQL Server database.


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

public class DataAccessExample
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";
        string query = "SELECT CustomerID, CompanyName, ContactName FROM Customers;";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();

            try
            {
                connection.Open();
                adapter.Fill(dataSet, "Customers"); // Fill the DataSet with a DataTable named "Customers"

                Console.WriteLine("Customer Data:");
                foreach (DataRow row in dataSet.Tables["Customers"].Rows)
                {
                    Console.WriteLine($"ID: {row["CustomerID"]}, Name: {row["CompanyName"]}, Contact: {row["ContactName"]}");
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
            finally
            {
                // Connection is closed automatically by the 'using' statement
            }
        }
    }
}
        

Key Benefits

See Also: