|
|
Connecting to Database using MapPath Method:
This example will show you how to connect to an Access
database in ASP using MapPath Method.
<%
Option Explicit
' Dimension Local variables
Dim objConn ' Connection Name
Dim strConn ' Connection String
Dim objRS ' Recordset Variable
Dim strSQL ' variable for SQL statement
Dim intTotalColumns
Dim intCounter
Const adOpenStatic =
3
Const adLockReadOnly =
1
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS =
Server.CreateObject("ADODB.Recordset")
strConn = "DBQ=" & Server.MapPath("MyDatabase.mdb")
strConn = "DRIVER={Microsoft Access Driver(*.mdb)};" & strConn
objConn.Open strConn
strSQL = "SELECT * FROM Links"
objRS.open strSql, objConn, adOpenStatic, adLockReadOnly
' get the total number of columns
intTotalColumns = objRS.Fields.Count - 1
%>
<TABLE
BORDER="1" WIDTH="500">
<tr>
<%
' first display the
column names
For intCounter
= 0 To intTotalColumns
%>
<TD>
<B><%=objRS(intCounter).Name%></B>
</TD>
<%
Next
Response.write "</TR>"
' now loop through the
recordset and display the data
Do Until
objRS.EOF = True
Response.Write "<TR>"
For
intCounter = 0 To intTotalColumns
Response.Write "<td width=100 align=center>"
Response.write objRS(intCounter).value
Response.Write "</TD>"
Next
Response.Write "</TR>"
objRS.Movenext
Loop
%>
</TABLE>
<%
' Close Recordset
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>
|
|
|
|
|
|