ADO.NET API Reference
This section provides comprehensive documentation for the ADO.NET classes, which are part of the .NET Framework and .NET Core, offering a managed data access technology that exposes data manipulation and data access to the programmer.
Namespaces
ADO.NET functionalities are organized into several key namespaces:
System.Data
: The core namespace for ADO.NET.System.Data.Common
: Contains base classes for ADO.NET providers.System.Data.SqlClient
: Classes for connecting to Microsoft SQL Server.System.Data.OleDb
: Classes for accessing data sources through OLE DB.System.Data.Odbc
: Classes for accessing data sources through ODBC.System.Data.OracleClient
: Classes for connecting to Oracle databases (deprecated in .NET Core).
System.Data
Namespace
This namespace is the foundation of ADO.NET. It includes fundamental classes for working with data, such as:
DataSet
: Represents an in-memory cache of data retrieved from a data source.DataTable
: Represents a table of data in memory.DataRow
: Represents a row of data within aDataTable
.DataColumn
: Represents a column in aDataTable
.ConstraintCollection
: A collection ofConstraint
objects for aDataTable
.ForeignKeyConstraint
: Enforces referential integrity between two tables.UniqueConstraint
: Enforces uniqueness for values in a column or set of columns.
Key Classes:
// Example of creating a DataTable
using System.Data;
DataTable customersTable = new DataTable("Customers");
customersTable.Columns.Add("CustomerID", typeof(int));
customersTable.Columns.Add("CustomerName", typeof(string));
customersTable.Columns.Add("ContactName", typeof(string));
customersTable.PrimaryKey = new DataColumn[] { customersTable.Columns["CustomerID"] };
customersTable.Rows.Add(1, "Alfreds Futterkiste", "Maria Anders");
customersTable.Rows.Add(2, "Ana Trujillo Emparedados y helados", "Ana Trujillo");
System.Data.Common
Namespace
Provides classes that are common to all ADO.NET data providers, allowing for data access independent of the specific database. Key classes include:
DbConnection
: Abstract base class for database connections.DbCommand
: Abstract base class for database commands.DbDataReader
: Abstract base class for forward-only, read-only results.DbParameter
: Abstract base class for parameters.DbTransaction
: Abstract base class for database transactions.
System.Data.SqlClient
Namespace
Contains classes specific to Microsoft SQL Server, such as:
SqlConnection
: Represents a connection to a Microsoft SQL Server database.SqlCommand
: Represents a Transact-SQL statement or stored procedure to execute.SqlDataReader
: Provides a way of reading a forward-only stream of rows from a SQL Server data source.SqlDataAdapter
: Fills aDataSet
and resolves changes.SqlParameter
: Represents a parameter to aSqlCommand
.
Example Usage:
using System.Data.SqlClient;
using System.Data;
string connectionString = "Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT CustomerID, CustomerName FROM Customers WHERE Country = 'UK'";
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"ID: {reader["CustomerID"]}, Name: {reader["CustomerName"]}");
}
}
}
}
System.Data.OleDb
Namespace
Provides classes for accessing data sources through the OLE DB data provider. This includes:
System.Data.Odbc
Namespace
Provides classes for accessing data sources through the ODBC data provider. This includes: