Articles
DevASP - ASP and XML Articles, Samples, Toturials, Sample Chapters and resources for Developers Friday, November 28, 2014
Home
Articles & Samples
Dev Search
Sample Chapters
Link to US
Contact
Search Directory
Applications
Articles & Samples
Components
Community
Database
Developer Sites
Downloads
Hosting Services
Introduction
Knowledge Base
Sample Chapters
WebCasts
ASP Directory
Applications
Articles & Samples
Components
Developer Sites
Knowledge Base
Sample Chapters
WebCasts
XML Directory
Applications
Articles & Samples
Developer Sites
Error, Bugs & Fixes
Downloads
Introduction
Knowledge Base
Sample Chapters
WebCasts

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
%>

DevASP - Privacy - Disclaimer
Copyright 2008 DevASP.com