MSDN Documentation

Microsoft Developer Network

Understanding ADO.NET DataSets

The DataSet object is a core component of ADO.NET, representing an in-memory cache of data. It can hold multiple DataTable objects, each representing a distinct table of data, along with relations, constraints, and other information necessary to manage a complete relational data structure.

What is a DataSet?

A DataSet is a collection of DataTable objects that are independently persisted. It can also contain DataRelation objects that link tables together, and Constraint objects that enforce business rules, such as uniqueness and referential integrity.

Key characteristics of a DataSet:

Key Components of a DataSet

A DataSet is comprised of several key collections:

Creating and Populating a DataSet

You can create a DataSet and populate it using a DataAdapter:


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

// Assume connectionString and SQL query are defined
string connectionString = "Your_Connection_String";
string query = "SELECT CustomerID, CompanyName FROM Customers";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
    DataSet customerDataSet = new DataSet("Customers"); // Optional: Name the DataSet

    connection.Open();
    adapter.Fill(customerDataSet, "CustomerData"); // Populate and name the DataTable within the DataSet

    // Now customerDataSet.Tables["CustomerData"] contains the data
    foreach (DataRow row in customerDataSet.Tables["CustomerData"].Rows)
    {
        Console.WriteLine($"ID: {row["CustomerID"]}, Name: {row["CompanyName"]}");
    }
}
            

Modifying Data in a DataSet

You can directly modify the data within a DataTable contained in a DataSet:


// Assuming customerDataSet and its DataTable "CustomerData" are populated
DataRow newRow = customerDataSet.Tables["CustomerData"].NewRow();
newRow["CustomerID"] = 101;
newRow["CompanyName"] = "New Tech Solutions";
customerDataSet.Tables["CustomerData"].Rows.Add(newRow);

// To update an existing row:
DataRow existingRow = customerDataSet.Tables["CustomerData"].Rows[0]; // Get the first row
existingRow["CompanyName"] = "Acme Corporation (Updated)";
existingRow.AcceptChanges(); // Mark changes as accepted
            

Working with Relations

DataRelation objects allow you to define parent-child relationships between tables in a DataSet, enabling easier navigation and data integrity checks.

Note: While DataSet is powerful for disconnected scenarios and complex data manipulation, consider lighter-weight options like DataTable or ORMs (Object-Relational Mappers) like Entity Framework for simpler or performance-critical applications.

Serialization

DataSet objects can be easily serialized to and deserialized from XML:


// Serialize to XML
string xmlData = customerDataSet.GetXml();

// Deserialize from XML
DataSet loadedDataSet = new DataSet();
loadedDataSet.ReadXml(new System.IO.StringReader(xmlData));
            

Use Cases

Important Considerations:
  • DataSet objects can consume significant memory, especially when dealing with large amounts of data.
  • Managing the state of changes (added, modified, deleted rows) is crucial for updating the data source correctly.
  • For read-only scenarios or simple data retrieval, DataReader is often more efficient.