Other Methods of Connecting to a Microsoft Access Database

Recall that earlier in this chapter (see Hands-On 28-5) we created a File DSN in order to connect to the Northwind sample database. The following sections demonstrate other methods of establishing a connection to your database:

■ A DSN-less connection

■ A connection via OLE DB Provider

The above connection methods were already discussed in Part II of this book. They are mentioned here again so that you feel comfortable using them in the context of the ASP files.

Part V

Establishing a DSN-less Connection

You can open a data source without creating a File or System DSN by specifying the DRIVER and DBQ parameters in the connection string.

For example, if you want to use a DSN-less connection, you should revise the Employees.asp file like this: <%

If IsObject(Session("Northwind_conn")) Then Set conn = Session("Northwind_conn")

Else

Set conn = Server.CreateObject("ADODB.Connection") conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" &_

"C:\Learn_ASP\Northwind.mdb" Set Session("Northwind_conn") = conn

End If

In the above connection string, the DRIVER parameter specifies the Microsoft Access driver, and the DBQ parameter holds the path to the Microsoft Access database file. The above script creates an instance of the ADO Connection object and opens a connection to the Northwind database located in the Learn_ASP folder on the C: drive by using a Microsoft Access driver.

Connecting to a Microsoft Access Database Using OLE DB

You can also connect to an Access database by using a native OLE DB provider for Microsoft Access — Microsoft.Jet.OLEDB.4.0. When using an OLE DB connection, you must specify the provider name in the Provider parameter and provide the path to the Access database in the Data Source parameter. For example, if you want to use the OLE DB provider, you should revise the Employees.asp file like this:

If IsObject(Session("Northwind_conn")) Then Set conn = Session("Northwind_conn")

Else

Set conn = Server.CreateObject("ADODB.Connection") conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _

"C:\Learn_ASP\Northwind.mdb" Set Session("Northwind_conn") = conn

End If

The above script creates an instance of the ADO Connection object and opens a connection to the Northwind database by using an OLE DB connection string.

Taking Your VBA Programming Skills to the Web

0 0

Post a comment