Microsoft Docs

The ADO.NET Connection Object

The ADO.NET Connection object represents a unique session with a data source. The Connection object is the first step in establishing a connection to a database. It provides the context for interactions with the database, allowing you to execute commands and retrieve data.

Purpose and Functionality

The primary role of the Connection object is to manage the communication channel between your application and the database. Key functionalities include:

  • Establishing a connection using a connection string.
  • Opening and closing the connection.
  • Managing transactions.
  • Creating Command objects that are associated with the connection.
  • Retrieving database metadata.

Connection Strings

A connection string is a string that contains various parameters required to establish a connection to a data source. These parameters typically include:

  • Data source (server name or IP address).
  • Initial catalog (database name).
  • Authentication credentials (user ID and password).
  • Provider-specific information.

Here's an example of a typical SQL Server connection string:

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

For other data providers, such as OLE DB or ODBC, the connection string format will vary. ADO.NET provides specific classes for different data providers, such as SqlConnection for SQL Server, OleDbConnection for OLE DB, and OdbcConnection for ODBC.

Opening and Closing Connections

It's crucial to manage the lifecycle of your connections effectively. An opened connection consumes resources. You should always close your connections when they are no longer needed. The Open() method establishes the connection, and the Close() method terminates it.

Using a using statement is the recommended approach for ensuring that connections are properly disposed of, even if exceptions occur:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    // Perform database operations here
} // Connection is automatically closed and disposed here

Transaction Management

The Connection object is also responsible for managing transactions. Transactions allow you to group multiple database operations into a single logical unit of work. If any operation within the transaction fails, the entire transaction can be rolled back, ensuring data consistency.

You can start a transaction using the BeginTransaction() method and then commit or roll back the transaction:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    SqlTransaction transaction = connection.BeginTransaction();
    try
    {
        // Execute commands within the transaction
        // ...
        transaction.Commit(); // Commit the transaction if successful
    }
    catch (Exception ex)
    {
        transaction.Rollback(); // Roll back if an error occurs
        Console.WriteLine($"Error: {ex.Message}");
    }
}

Provider-Specific Connection Objects

ADO.NET follows a simple architecture that allows it to work with various data sources. This is achieved through the use of data providers. Each data provider has its own set of classes for managing connections, commands, and data. For example:

  • SQL Server: Uses classes from the System.Data.SqlClient namespace (e.g., SqlConnection, SqlCommand).
  • OLE DB: Uses classes from the System.Data.OleDb namespace (e.g., OleDbConnection, OleDbCommand).
  • ODBC: Uses classes from the System.Data.Odbc namespace (e.g., OdbcConnection, OdbcCommand).

Always use the appropriate connection object for the data provider you are interacting with.