Microsoft Learn

Introduction to ADO.NET

Welcome to the ADO.NET documentation. ADO.NET is a set of .NET Framework classes that expose data access services to .NET programmers. ADO.NET is part of the .NET Framework and is used to access data sources such as relational databases.

ADO.NET provides a rich set of components for creating distributed applications and managing data from multiple data sources. It supports various data sources, including SQL Server, Oracle, and other OLE DB-compliant databases. ADO.NET can also be used to access XML data.

Key Concepts in ADO.NET

ADO.NET is built around a set of core objects that facilitate data retrieval, manipulation, and storage. The most fundamental objects include:

ADO.NET Architecture

ADO.NET applications typically use two main architectural patterns:

  1. Connected Data Access: This pattern involves establishing a direct connection to the data source for the duration of the data operation. The DataReader object is commonly used in this model for its efficiency.
    // Example of connected data access (conceptual)
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand("SELECT * FROM Customers", connection);
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                // Process data from reader
            }
        }
    }
  2. Disconnected Data Access: This pattern involves retrieving data into a DataSet, closing the connection to the data source, and then working with the data in memory. Changes are then committed back to the data source using a DataAdapter. This is ideal for applications that need to operate without a constant connection, such as mobile or web applications.
    // Example of disconnected data access (conceptual)
    DataSet dataSet = new DataSet();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Products", connection);
        adapter.Fill(dataSet);
    }
    // Work with dataSet in memory
    // Later, to update:
    // adapter.Update(dataSet);

Note: The choice between connected and disconnected access depends on your application's requirements for performance, data manipulation needs, and the necessity of maintaining an active connection.

This introduction provides a foundational understanding of ADO.NET. As you delve deeper, you will explore topics such as error handling, transactions, data binding, and working with different data providers.

Next: DataSet Overview Introduction to ADO.NET