MSDN Documentation

System.Data Namespace

Namespace: System.Data

Assembly: System.Data.dll

Provides classes for working with data in disconnected and connected modes, including data sets, data tables, data rows, and data columns.

Classes

Enums

Common Usage Example


using System.Data;

// Create a DataSet
var dataSet = new DataSet("MySampleDataSet");

// Create a DataTable
var dataTable = new DataTable("Customers");

// Add columns to the DataTable
dataTable.Columns.Add("CustomerID", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("City", typeof(string));

// Add rows to the DataTable
var row1 = dataTable.NewRow();
row1["CustomerID"] = 1;
row1["Name"] = "Alice Smith";
row1["City"] = "New York";
dataTable.Rows.Add(row1);

var row2 = dataTable.NewRow();
row2["CustomerID"] = 2;
row2["Name"] = "Bob Johnson";
row2["City"] = "London";
dataTable.Rows.Add(row2);

// Add the DataTable to the DataSet
dataSet.Tables.Add(dataTable);

// Accessing data
foreach (DataRow row in dataSet.Tables["Customers"].Rows)
{
    var id = row["CustomerID"];
    var name = row["Name"];
    var city = row["City"];
    // Console.WriteLine($"ID: {id}, Name: {name}, City: {city}");
}

// You would typically use DbConnection and DbCommand to populate the DataSet from a database.
// Example with SqlDataReader (conceptually):
// using (var connection = new SqlConnection("your_connection_string")) {
//     connection.Open();
//     using (var command = new SqlCommand("SELECT * FROM Customers", connection)) {
//         using (var reader = command.ExecuteReader()) {
//             dataTable.Load(reader);
//         }
//     }
// }
                

Explore the members of the System.Data namespace to understand how to effectively manage and manipulate data within your .NET applications.