The Connection object establishes a connection to a data source. It allows your application to pass client information, such as username and password, to the database for validation.
Once you have completed these steps, you will be ready to establish a connection using the Open method.
Before you can use ADO in your Visual Basic application, you must first set a reference to the Microsoft ActiveX Data Objects 2.0 Library.
Once you have made a reference to the ADO object library, you can declare a Connection object in your application. Using the Connection object, you can then create a Command object or Recordset object.
The following example code declares and instantiate a new Connection object:
Dim cnStateUBookstore As Connection
Set cnStateUBookstore = New Connection
Once you have instantiated a Connection object, you must specify an OLE DB data source provider. You do this by setting the Provider property.
The following example code specifies the Microsoft SQL Server OLE DB data provider:
cnStateUBookstore.Provider = "SQLOLEDB"
The final step before establishing a connection to a data source is to specify the connection information. You do this by setting the Connection object's ConnectionString property. Connection string arguments are provider specific, are passed directly to the provider, and are not processed by ADO.
For more information about connection string syntax, read the article "ConnectionString Property (ADO)" in ADO Help.
The following connection string arguments are used with the SQL Server OLE DB provider.
Connection argument | Description |
---|---|
User ID | Valid user name |
Password | Valid user password |
Data Source | Name of the remote server |
Initial Catalog | Database name in the external data source |
The following example code specifies a Microsoft SQL Server data provider and supplies connection information in the ConnectionString property:
With cnStateUBookstore
.Provider = "SQLOLEDB"
.ConnectionString = "User ID=sa;Password=;" & _
"Data Source=MSERIES1;" & _
"Initial Catalog=StateUBookstore"
End With
Password=;
used to signify a blank password is redundant since this is the default value for this argument.
The following table describes some of the OLE DB providers currently available.
OLE DB data provider | Description |
---|---|
SQLOLEDB | OLE DB provider for Microsoft SQL Server |
MSDASQL | OLE DB provider for ODBC |
Microsoft.Jet.OLEDB.3.51 | OLE DB provider for Microsoft Jet |
MSIDXS | OLE DB provider for Microsoft Index Server |
ADSDSOObject | OLE DB provider for Microsoft Active Directory Service |
MSDAORA | OLE DB provider for Oracle |
For more information about specific OLE DB data providers, read the article "Using OLE DB Providers with ADO and RDS" in Platform SDK Help.