This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".
As you're seen in figure 5-1, the first data provider that interacts with a data source is the Connection object. You can bypass a Connection object by using a DataProvider directly, but in that case a DataProvider uses the connection internally. (I'll discuss the DataAdapter later in this article.) The Connection class has a different name depending upon the Data Provider. As you can see from figure 5-21, the connection class for OleDb, Sql, and ODBC are OleDbConnection, SqlConnection, OdbcConnection, respectively. All data provider connection classes implement the IDbConnection interace, which represents a unique session with a data source (see figure 5-21).
Figure 5-21. The IDb connection and its derived classes
The Connection class has a connection string that opens a connection to the database. The connection string will vary depending upon the provider used. The connection strings typically contain a group of property-value pair to describe how to connect to a database. For an OleDbConnection, you have properties such as Provider and DataSource. Table 5-16 describes the Connection class properties. Based on the data provider, some of these properties may not be applicable.
Table 5-16. Connection object properties
|
PROPERTY |
DESCRIPTION |
|
ConnectionString |
Represent the connection string. |
|
ConnectionTimeOut |
Waiting time while establishing a connection. |
|
DataBase |
Name of the current database. |
|
DataSource |
Location of the file name of the data source. |
|
Provider |
Name of the OLE DB provider. This property is not available for Sql and ODBC data providers. |
| State | Current state of the connection of type ConnectionState. (Table 5-17 describes the ConnectionState). |
|
PacketSize |
Size of network packets. Available to only Sql data providers. |
|
ServerVersion |
SQL server version. Available to only Sql data providers. |
|
WorkStationId |
Database client ID. Available to only Sql data providers. |
The connection can have different states such as open, closed, connecting, and so on. The ConnectionType enumeration defines the members of the ConnectionState. Table 5-17 describes its members.
Table 5-17. The ConnectionType Enumeration members
|
MEMBER |
DESCRIPTION |
|
Broken |
Connection is broken after it was opened. May cause by network failure. |
|
Closed |
Connection is closed. |
|
Connecting |
Opening a new connection. |
|
Executing |
The connection is executing a command. |
|
Fetching |
Retrieving data from a data source. |
|
Open |
Connection is open and ready to use. |



Join the conversation! Your thoughts help the community grow.