MSDN Documentation

ADO.NET Data Connection Concepts

This document explores the fundamental concepts of data connections within the ADO.NET framework. Understanding how to establish, manage, and utilize connections is crucial for interacting with data sources efficiently and securely.

The Role of the Connection Object

The primary object responsible for representing a connection to a data source in ADO.NET is the Connection object. This object provides the interface for communicating with the database.

Different data providers expose their own specialized Connection classes:

Connection Strings

A connection string is a string that contains information required to establish a connection to a data source. This information typically includes the server name, database name, authentication credentials, and other provider-specific parameters.

Example connection string for SQL Server:


Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
            

It is highly recommended to store connection strings securely, rather than hardcoding them directly into the application code. This can be achieved through configuration files (e.g., appsettings.json in .NET Core/5+) or environment variables.

Opening and Closing Connections

Connections must be explicitly opened before they can be used to execute commands or retrieve data, and closed when they are no longer needed.


using System.Data.SqlClient;

public void ConnectToDatabase()
{
    string connectionString = "Server=myServerAddress;Database=myDataBase;Integrated Security=True;";
    SqlConnection connection = new SqlConnection(connectionString);

    try
    {
        connection.Open();
        Console.WriteLine("Connection opened successfully!");
        // Perform database operations here
    }
    catch (SqlException ex)
    {
        Console.WriteLine($"Error connecting to database: {ex.Message}");
    }
    finally
    {
        if (connection.State == System.Data.ConnectionState.Open)
        {
            connection.Close();
            Console.WriteLine("Connection closed.");
        }
    }
}
            

The using statement is the preferred way to manage disposable objects like SqlConnection, as it ensures that the Dispose() method (which calls Close()) is automatically invoked, even if exceptions occur.

Connection Pooling

Connection pooling is a technique used by ADO.NET to improve application performance by reusing database connections. Instead of establishing a new connection every time one is needed, a pool of connections is maintained. When an application requests a connection, it is retrieved from the pool. When the connection is closed, it is returned to the pool rather than being physically disconnected from the database.

Connection pooling is enabled by default for most ADO.NET data providers. You can configure its behavior through specific keywords in the connection string.

Connection States

A Connection object has a State property that indicates its current status. The possible states include:

Key Classes and Interfaces

Best Practices