Articles  
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Search Directory
ASP Directory
Xml Directory

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Connect to Access database with MapPath Function

Author: Asif

Connecting to an Access database without DSN Connection, Using MapPath Function of Server Variables.


<%
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
%>
  &lt;TABLE BORDER="1" WIDTH="500">
  &lt;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
%>

 

Disclaimer - Privacy
© 2002-2014 DevASP.com