ADO.NET is a set of .NET Framework classes that expose data access services to. NET programmers. ADO.NET is composed of two main parts: the DataSet and the Data Provider.
The ADO.NET architecture allows you to build application components that efficiently interact with data sources, such as relational databases, at the same time that you build services that manage data manipulation and distribution.
The DataSet
object represents a collection of tables, relationships, and constraints for data within an application. It is an in-memory representation of data, allowing you to work with data independently of the data source.
DataTable
object represents a single table of data in memory. It contains columns (DataColumn
) and rows (DataRow
).DataRelation
objects define relationships between tables in the DataSet
, similar to foreign key relationships in databases.Constraint
objects (like UniqueConstraint
and ForeignKeyConstraint
) enforce data integrity rules within the DataSet
.The DataSet
is designed to work with data from multiple sources, including SQL Server, Oracle, and XML. It can also be used to manipulate data that has not been retrieved from a data source, such as data generated programmatically.
A Data Provider is a set of .NET Framework classes that plug into ADO.NET to expose data manipulation capabilities for a data source. Each Data Provider for a given data source provides a set of objects that expose the commands, connections, and data readers that are specific to that data source.
The common .NET data providers include:
DataSet
and a data source. It is used to populate a DataSet
and reconcile changes made to the data in the DataSet
back to the data source.A common workflow for interacting with a database using ADO.NET involves the following steps:
SqlConnection
) and open it.SqlCommand
) with a SQL statement or stored procedure name and associate it with the connection.DataReader
to read the results row by row, or use a DataAdapter
to fill a DataSet
.ExecuteNonQuery()
method.using
statements to ensure connections are properly closed and disposed of.Here's a simplified C# example of retrieving data using a DataReader
:
using System;
using System.Data;
using System.Data.SqlClient;
public class DataRetriever
{
public void GetData(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "SELECT CustomerID, CompanyName FROM Customers";
using (SqlCommand command = new SqlCommand(sql, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"ID: {reader["CustomerID"]}, Name: {reader["CompanyName"]}");
}
}
}
}
}
}
ADO.NET supports both connected and disconnected data access scenarios:
DataReader
.DataSet
. Changes made to the DataSet
are then reconciled back to the data source when the connection is reopened. This is ideal for applications that need to work with data without holding a connection open constantly, such as web applications.