[ Team LiB ] Previous Section Next Section

IDbConnection disposable

System.Data (system.data.dll) interface

This class represents a connection to a data source, which can be opened using the Open( ) method or closed using Close( ) . No work can be performed while a connection is closed; however, some objects (such as data adapters) automatically open and close their connection as required. A data provider derives its own Connection class (such as System.Data.SqlClient.SqlConnection ) that implements this interface.

With most data providers, you need to set some basic information (such as the initial database, server, and user login account) in the ConnectionString property before you attempt to open a connection. The connection string information differs from provider to provider. All connection strings arrange their information in a series of name-value pairs delimited by semicolons. Other than ConnectionString , the other IDbConnection properties are read-only. Chapter 3 provides connection string examples for several provider types and a list of supported parameters.

The IDbConnection interface also defines a BeginTransaction( ) method, which initiates a SQL transaction and returns an IDbTransaction object that allows you to commit or roll back the transaction. You can also use ChangeDatabase( ) to work with the tables in a different database in the same data source, and CreateCommand( ) to generate a generic IDbCommand instance linked to the current connection. You don't need to use CreateCommand( ) ; you can instantiate the appropriate provider-specific Command object. However, the CreateCommand( ) method is useful when you need to write provider-agnostic ADO.NET code. Even though it returns the appropriate provider-specific Command object, you can interact with it solely through the IDbConnection and IDbCommand interfaces, freeing your code from provider-specific details.

public interface IDbConnection : IDisposable {
// Public Instance Properties
   public string ConnectionString{set; get; } 
   public int ConnectionTimeout{get; } 
   public string Database{get; } 
   public ConnectionState State{get; } 
// Public Instance Methods
   public IDbTransaction BeginTransaction(  );  
   public IDbTransaction BeginTransaction(IsolationLevel il);  
   public void ChangeDatabase( string databaseName);  
   public void Close(  );  
   public IDbCommand CreateCommand(  );  
   public void Open(  );  
}

Implemented By

System.Data.OleDb.OleDbConnection , System.Data.OracleClient.OracleConnection , System.Data.SqlClient.SqlConnection

Returned By

IDbCommand.Connection , IDbTransaction.Connection

Passed To

IDbCommand.Connection

    [ Team LiB ] Previous Section Next Section