MSDN Documentation

Datasets in ADO.NET

A DataSet is an in-memory representation of data, typically from a relational data source. It is a collection of DataTable objects, which in turn contain DataRow objects representing records and DataColumn objects defining the schema. DataSet objects are particularly useful for working with disconnected data scenarios.

Key Characteristics of a DataSet

Creating and Populating a DataSet

You can create a DataSet programmatically or populate it using a DataAdapter.

Programmatic Creation:

Manually define tables, columns, and add rows.


using System.Data;

DataSet myDataSet = new DataSet("MySampleData");

DataTable employeesTable = new DataTable("Employees");
employeesTable.Columns.Add("EmployeeID", typeof(int));
employeesTable.Columns.Add("FirstName", typeof(string));
employeesTable.Columns.Add("LastName", typeof(string));
employeesTable.PrimaryKey = new DataColumn[] { employeesTable.Columns["EmployeeID"] };

employeesTable.Rows.Add(1, "Nancy", "Davolio");
employeesTable.Rows.Add(2, "Andrew", "Fuller");

myDataSet.Tables.Add(employeesTable);

// You can add more tables and define relationships
            

Populating with a DataAdapter:

This is the most common method, where a DataAdapter (like SqlDataAdapter or OleDbDataAdapter) fills the DataSet from a database.


using System.Data;
using System.Data.SqlClient; // For SQL Server

// Assuming connectionString is properly defined
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter("SELECT EmployeeID, FirstName, LastName FROM Employees", connection);
DataSet employeeDataSet = new DataSet();

adapter.Fill(employeeDataSet, "Employees");
            

Working with DataTables within a DataSet

Once populated, you can access individual DataTable objects by name or index.


DataTable employees = myDataSet.Tables["Employees"]; // Or myDataSet.Tables[0];

foreach (DataRow row in employees.Rows)
{
    Console.WriteLine($"ID: {row["EmployeeID"]}, Name: {row["FirstName"]} {row["LastName"]}");
}
            

Managing Changes and Updates

DataSet keeps track of the original and current versions of rows, enabling effective tracking of modifications.

Note: While powerful for disconnected scenarios, DataSet can consume significant memory for large datasets. For scenarios requiring read-only, forward-only access to data, consider using DataReader objects for better performance.

DataSet vs. DataTable

A DataTable represents a single table of data, whereas a DataSet can contain multiple, related DataTable objects.

Summary

The DataSet is a cornerstone of ADO.NET for managing relational data in memory. Its ability to handle disconnected data, support constraints, and integrate with XML makes it a versatile tool for many data-driven applications.