IDbConnection disposable

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.