System.Data.DataSet

An in-memory representation of relational data.

Introduction to DataSet

The System.Data.DataSet class represents a complete set of data, including related tables, constraints, and relationships. It is an in-memory cache of data that can be used to store and manipulate data from multiple sources, such as relational databases.

A DataSet contains a collection of DataTable objects, each representing a table of data. These tables can be related to each other through DataRelation objects, mimicking the structure of a relational database.

Key Features and Benefits

Core Components

Common Operations

Creating a DataSet

You can create a new DataSet instance as follows:

using System.Data;

DataSet myDataSet = new DataSet("MyAppData");

Adding a DataTable to a DataSet

First, you'll typically create and configure a DataTable:

DataTable customersTable = new DataTable("Customers");

// Define columns
DataColumn idColumn = new DataColumn("CustomerID", typeof(int));
idColumn.AutoIncrement = true;
idColumn.AutoIncrementSeed = 1;
idColumn.Unique = true; // Primary Key constraint
customersTable.Columns.Add(idColumn);

customersTable.Columns.Add("FirstName", typeof(string));
customersTable.Columns.Add("LastName", typeof(string));

// Set primary key
customersTable.Constraints.Add(new UniqueConstraint(idColumn));

// Add the DataTable to the DataSet
myDataSet.Tables.Add(customersTable);

Adding Data to a DataTable

// Create a new row
DataRow newRow = customersTable.NewRow();
newRow["FirstName"] = "John";
newRow["LastName"] = "Doe";

// Add the row to the table
customersTable.Rows.Add(newRow);

// Add another row
customersTable.Rows.Add(new object[] { "Jane", "Smith" }); // Using object array for simplicity

Querying Data

You can use LINQ to DataSet or direct filtering:

// Using DataView for filtering and sorting
DataView customerView = new DataView(myDataSet.Tables["Customers"]);
customerView.RowFilter = "LastName = 'Doe'";

foreach (DataRowView rowView in customerView)
{
    Console.WriteLine($"Found: {rowView["FirstName"]} {rowView["LastName"]}");
}

// Using LINQ to DataSet (requires System.Data.DataSetExtensions)
var filteredCustomers = from DataRow row in myDataSet.Tables["Customers"].AsEnumerable()
                        where row.Field("LastName") == "Smith"
                        select new { FirstName = row.Field("FirstName"), LastName = row.Field("LastName") };

foreach (var customer in filteredCustomers)
{
    Console.WriteLine($"LINQ Found: {customer.FirstName} {customer.LastName}");
}

Working with Multiple Tables and Relations

To represent relationships, you can add DataRelation objects:

// Assume OrdersTable is also created and added to myDataSet
            // with an OrderID and CustomerID column

            // Define the relationship between Customers and Orders
            DataColumn customerIdCol = myDataSet.Tables["Customers"].Columns["CustomerID"];
            DataColumn customerFkCol = myDataSet.Tables["Orders"].Columns["CustomerID"];

            DataRelation relation = new DataRelation("FK_Customer_Orders", customerIdCol, customerFkCol);
            myDataSet.Relations.Add(relation);

            // Now you can navigate relations
            DataRow customerRow = myDataSet.Tables["Customers"].Rows[0]; // Assuming John Doe
            DataRow[] ordersForCustomer = customerRow.GetChildRows(relation);

            Console.WriteLine($"Orders for {customerRow["FirstName"]} {customerRow["LastName"]}:");
            foreach (DataRow order in ordersForCustomer)
            {
                Console.WriteLine($"  Order ID: {order["OrderID"]}");
            }

Performance Considerations

While powerful, DataSet can consume significant memory. For simple data retrieval, consider using DataReader for better performance. Use DataSet when you need to work with complex, disconnected data models and perform extensive manipulations in memory.