ADO.NET Basics

This tutorial covers the fundamental concepts and components of ADO.NET, the data access technology in the .NET Framework. ADO.NET provides a rich set of classes for working with data from various sources, primarily relational databases.

Key Components of ADO.NET

ADO.NET consists of several core namespaces and classes that work together to enable data access. The most important ones include:

The ADO.NET Object Model

The ADO.NET object model is designed around two main categories of objects:

  1. Connected Data Access Objects: These objects are used to establish a connection to a data source, execute commands, and retrieve data in a forward-only, read-only manner.
  2. Disconnected Data Access Objects: These objects allow you to retrieve data and then work with it without maintaining an active connection to the data source. This is useful for applications that need to perform operations on data independently of the database.

Connected Data Access Objects

The primary objects for connected data access are:

Disconnected Data Access Objects

The primary objects for disconnected data access are:

A Simple ADO.NET Example

Let's look at a basic example of how to connect to a SQL Server database and retrieve data using a SqlDataReader.

C# Example: Connecting and Reading Data
using System;
using System.Data;
using System.Data.SqlClient;

public class AdonetBasics
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;";
        string queryString = "SELECT ProductID, ProductName, UnitPrice FROM dbo.Products;";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            try
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Console.WriteLine($"ID: {reader["ProductID"]}, Name: {reader["ProductName"]}, Price: {reader["UnitPrice"]}");
                    }
                }
                else
                {
                    Console.WriteLine("No rows found.");
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }
}
Note: Remember to replace placeholder values in the connection string with your actual database credentials and server details.

Common Operations

Key operations you'll perform with ADO.NET include:

Tip: Always use the using statement for ADO.NET objects like Connection, Command, and DataReader to ensure resources are properly disposed of, even if exceptions occur.

This overview provides a foundation for understanding ADO.NET. The following sections will delve deeper into specific components and advanced techniques.