.NET Framework: ADO.NET Data Access

Introduction to ADO.NET

ADO.NET is a set of .NET classes that expose data access services to the .NET Framework. It is a fundamental part of the .NET platform for data access, enabling developers to connect to data sources, execute commands, and retrieve result sets. ADO.NET provides a rich set of components for working with data from various sources, including relational databases, XML, and other data stores.

Key benefits of using ADO.NET include:

Core Components of ADO.NET

ADO.NET is built around a set of core objects that work together to facilitate data access. These components can be broadly categorized into two groups:

1. Connected Data Access (Provider Model)

This model establishes a continuous connection to the data source. It's suitable for tasks where real-time data is crucial or when performing operations that require an open connection.

Example of connected data access:


using System.Data;
using System.Data.SqlClient;

// Assume connectionString is properly defined
string connectionString = "Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    string sqlQuery = "SELECT CustomerID, CompanyName FROM Customers";

    using (SqlCommand command = new SqlCommand(sqlQuery, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine($"ID: {reader["CustomerID"]}, Name: {reader["CompanyName"]}");
            }
        }
    }
}
            

2. Disconnected Data Access (Dataset Model)

This model allows applications to retrieve data from a data source, keep it in memory using a DataSet, and then close the connection. Modifications made to the DataSet can be later reconciled with the data source. This is ideal for working with data in a UI, such as in data-bound controls.

Example of disconnected data access:


using System.Data;
using System.Data.SqlClient;

string connectionString = "Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;";
string sqlQuery = "SELECT OrderID, OrderDate FROM Orders";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, connection);
    DataSet dataSet = new DataSet();

    adapter.Fill(dataSet, "Orders"); // Fills the DataSet with data from the query into a table named "Orders"

    // Now you can work with dataSet.Tables["Orders"] without an open connection
    foreach (DataRow row in dataSet.Tables["Orders"].Rows)
    {
        Console.WriteLine($"Order ID: {row["OrderID"]}, Date: {row["OrderDate"]}");
    }
}
            

Key Classes and Concepts

DbProviderFactories

A factory class for creating instances of ADO.NET data providers dynamically.

DbConnection

The abstract base class for ADO.NET connection objects.

DbCommand

The abstract base class for ADO.NET command objects.

DbDataReader

The abstract base class for ADO.NET data reader objects.

DataSet

Represents an in-memory cache of data.

DataTable

Represents a table of data in memory.

DbDataAdapter

The abstract base class for ADO.NET data adapter objects.

DbParameter

Represents a parameter for a DbCommand.

Best Practices

To ensure efficient and secure data access with ADO.NET, consider the following: