3 Opening a Database Connection for use in an ASP
Page
Back to Tutorial
3.1 Open a DSN-less Connection using a FrontPage Connection
After Creating a FrontPage Connection as detailed in Part
2, you can use the connection name that you created to link the database
with your ASP page.
If you cannot remember the name of your connection, you can find it under the
Web Settings option in the Tools menu. (See Figure 1)
Figure 1
- Include adovbs.inc into the same directory as the page you are adding your
connection to by downloading adovbs.inc and using
the following code at the top of the web page where you adding the
connection code.
<!--#Include file="adovbs.inc"
-->
- To connect to the facultyConnection from Figure 1, the following code
should be placed at the top of the web page:
<%
Set fp_conn = Server.CreateObject("ADODB.Connection")
fp_conn.Open Application("facultyConnection_ConnectionString")
%>
- The '<%' and '%>' are signals to the server that the code inside of
these brackets is ASP script.
- The first line of ASP script creates a new connection
- The second line opens the facultyConnection. To open a different
connection just replace facultyConnection with the name of your connection.
- You should now be ready to use recordsets to interact with the database.
3.2 Open a DSN-less Connection without using a FrontPage
Connection
To use a DSN-less Connection it is not necessary to create a connection using
Microsoft FrontPage. The following code will allow you to connect to
an Access database.
- Include adovbs.inc into the same directory as the page you are adding your
connection to by downloading adovbs.inc and using
the following code at the top of the web page where you adding the
connection code.
<!--#Include file="adovbs.inc"
-->
- This following code contains the connection to the Access Database.
It can be placed at the top of any web pages needing to connect to the
database. You could connect to any type of database depending on how the
server is setup and the drivers you call for. This connection is called a
DSN-less connection to Access Database. DSN Stands for 'Data Source Name'.
<%
set conntemp=server.createobject("adodb.connection")
DSNtemp="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtemp=dsntemp & "DBQ=" & server.mappath("../../fpdb/OfficeList.mdb")
conntemp.Open DSNtemp
%>
- The '<%' and '%>' are signals to the server that the code inside of
these brackets is ASP script.
- The first line of ASP script creates a new connection
- The second and third line create a string containing the connection
information. This database is an Access database located at
"../../fpdb/OfficeList.mdb". To open a database with a
different name or location simply change the location in the third line.
- The last line opens your connection using the DSNtemp string.
- You should now be ready to use recordsets to interact with the database.