The properties of the connection object are:
A string used to connect to the database.
The number of seconds till a connection times out (Read Only)
Returns the database name as specified in connection string (Read Only)
Returns the source attribute as specified in connection string (Read Only)
Returns version of connected server.
Returns state of current database in integers. Values can be Closed, Connecting, Open, Executing, Fetching, Broken
Returns the value of provider attribute as specified in connection string (Read Only) (OleDb Only)
Returns size in bytes of network packets (SQL Server only)
Identifies client, as specified in connection string (Read Only)
In the above table, the only property that is NOT read only is the connection string. Some folks say that it is the connection string that is the most difficult aspect of ADO and ADO.Net. If so, it is an easily learned one. A typical connection string consists of 4 items:
The Provider, which specifies the name of the underlying OLEDB provider. Appropriate values are SQLOLEDB (for SQLServer), Microsoft.Jet.OLEDB.4.0 (for Microsoft Access) and MSDORA (for Oracle);
The Data Source attribute, which shows the location of the database. It can be a path on a network, or the IP address of a machine on the net;
The UserID and Password, which grant access permission to the database;
The Initial Catalog, which specifies the name of the database in the data source.
Here are some common configurations:
For SQL Server –
Data Source=Jupiter;Initial Catalog=pubs;User Id=ElmerFudd;Password=wabbitt;
Server=Jupiter;Database=pubs;Trusted_Connection=True;Connection Timeout = 10
Data Source=184.108.40.206;Network Library=Wiley3301;Initial Catalog=pubs;User ID=ElmerFudd;Password=wabbitt;
C#: obj VB.NET:
objqlConnection oSQLConn = new SqlConnection();oSQLConn.ConnectionString=connectstring;;oSQLConn.Open();
Dim objSQLConn As SqlConnection = New SqlConnection()
For Oracle: Provider=OraOLEDB.Oracle;Data Source=mydatabase;User Id=ElmerFudd;Password=wabbitt;
Provider=OraOLEDB.Oracle;Data Source= mydatabase;OSAuthent=1;
OracleConnection objOracleConn = new OracleConnection();
objOracleConn.ConnectionString = my connectionstring;
Dim objOracleConn As OracleConnection = New OracleConnection()
objOracleConn.ConnectionString = myconnectionstring
For MS Access:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\pathname\biblio.mdb;User Id=ElmerFudd;Password=wabbitt;
Notice that the last instruction in the code using the method ‘open()’. After the connection has been made, and the data retrieved, you need to close the connection using the connection method ‘close()’. This should be done within an ‘if’ statement which first checks whether the connection is, in fact, open:
If (objConnection.state and ConnectionState.Open) <>0 Then
Note that the state property is ‘0’ if the connection is already closed. Testing for a closed connection is necessary to prevent an error when you are invoking the ‘close’ method.
The connection objects methods are:
Begins database transaction
Changes the name of database connected to
Creates a command object
Returns schema tables and associated restricted columns
Shared method which allows closing of connection pool when last connection is closed
All ADO connection procedures should be protected with a Try/Catch Block. When dealing with a connection to another server, this is especially important to let your users know that it was the connection that failed, rather than the application code.
connSQLNorthwind.ConnectionString = _
"Server=Jupiter;Database=pubs;Trusted_Connection=True;Connection Timeout = 10"
Catch ExSQL As System.Data.SqlClient.SqlException
Dim strErrorMsg As String
Dim strerror As System.Data.SqlClient.SqlError
For Each strerror In ExSQL.Errors
Select Case strerror.Number
strErrorMsg = "Missing server"
strErrorMsg = "Missing database"
strErrorMsg = "Missing user name or password"
strErrorMsg = strerror.Message
MessageBox.Show(sErrorMsg, "SQL Server Error: " & strerror.Number, MessageBoxButtons.OK MessageBoxIcon.Error)
Catch ExcpInvOp As System.InvalidOperationException
MessageBox.Show("Close the connection first!", _
"Invalid Operation MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch Excp As System.Exception ' generic exception handler
MessageBox.Show(Excp.Message, "Unexpected Exception MessageBoxButtons.OK, MessageBoxIcon.Error)
This resource provides information on ADO.Net course
This is a useful resource that discusses .Net technology in detail.