Microsoft Learn

Your journey to proficiency starts here.

ADO.NET

ADO.NET is a set of .NET classes that expose data access services to the .NET programmer. ADO.NET provides consistent access to data sources such as SQL Server and XML, as well as data sources that can be accessed through OLE DB and ODBC. It enables you to write managed code that creates and manages data manipulation and data access.

Key Components

ADO.NET provides a rich set of components for working with data. The core objects include:

Core Concepts

Understanding these concepts is crucial for effective use of ADO.NET:

Connections and Commands

Establishing a connection is the first step to interacting with a data source. Once connected, you can execute commands to retrieve or manipulate data.

Example: Executing a Simple Query

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

public class Sample
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            string sql = "SELECT COUNT(*) FROM MyTable";
            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                int count = (int)command.ExecuteScalar();
                Console.WriteLine($"Number of rows: {count}");
            }
        }
    }
}

Data Readers

DataReader is ideal for scenarios where you need to iterate through a large result set quickly without loading it all into memory.

Example: Using DataReader

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

public class Sample
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;";
        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"]}");
                }
            }
        }
    }
}

DataSets

DataSet is a powerful tool for working with data offline or when you need to represent complex data structures.

Example: Populating a DataSet

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

public class Sample
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;";
        DataSet dataSet = new DataSet();

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string sql = "SELECT ProductID, ProductName, UnitPrice FROM Products";
            SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
            adapter.Fill(dataSet, "Products");
        }

        Console.WriteLine($"Table '{dataSet.Tables[0].TableName}' contains {dataSet.Tables[0].Rows.Count} rows.");
        foreach (DataRow row in dataSet.Tables["Products"].Rows)
        {
            Console.WriteLine($"Product: {row["ProductName"]}, Price: {row["UnitPrice"]}");
        }
    }
}

Data Providers

ADO.NET supports various data providers, each with its own set of classes:

Further Reading