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
-
DataSet
Represents an in-memory cache of data. It can hold multiple tables of data and relationships between them.
-
DataTable
Represents a table of data in memory.
-
DataRow
Represents a row of data within a DataTable.
-
DataColumn
Represents a column in a DataTable.
-
DataView
Provides a way to sort, filter, and search data in a DataTable.
-
IDbConnection
Represents a connection to a data source.
-
IDbCommand
Represents a Transact-SQL statement or stored procedure to execute against a data source.
-
IDataReader
Provides a way to read a forward-only stream of rows from a data source.
Enums
-
CommandBehavior
Specifies additional options for executing a command or query.
-
ConnectionState
Indicates the state of a connection to a data source.
-
MissingMappingAction
Specifies how a source table or column is mapped to a destination table or column when no explicit mapping is defined.
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.