Menu
  Home
  Rates
  Contact Us
  News Links
  Search
  User Pages
  Downloads
  Tutorials / FAQs
  Web Links
  Classifieds
  Site Map
  Tucker News
  Event Calendar
Web-Mail
 E-Mail address:
 
 Password: 
 
 
Phone Numbers
Dial Up:
(231) 796-5867
Tech Support:
(231) 796-2548
9:00am-5:00pm M-F
DSL Access
DSL! 
Starting At $39.95/Mo.  
Tucker Communications - Tutorial - DSN-Less Database

Introduction

There is more then one way of setting up databases (DB) for use with ASP.
This page shows how to setup a DB-connection to an MS Access datasource without a system DSN. It also shows an example of retrieving data with a SQL-select statement.
You can copy and paste the code to your own page, then make adjustments to the file name of your DB and SQL-statement. Make sure to save it as an .ASP page. This ASP page assumes that the database is in the same directory, and that the web-server supports ASP/vbscript.

Source code starts here:

<html>

<head>
<title>DSN-Less example</title>
</head>

<%
'-- Declare your variables
Dim DataConnection, SQLstring, myRecordSet, DBFileName
' Change the db1.mdb to <yourfilename>.mdb
DBFileName = "db1.mdb"

'-- Create dataconnection and recordset object and open database
Set DataConnection = Server.CreateObject("ADODB.Connection")
Set myRecordSet = Server.CreateObject("ADODB.Recordset")

' The line below shows how to use a system DSN instead of a DSN-LESS connection
' DataConnection.Open "DSN=System_DSN_Name"

DataConnection.Open "DBQ=" & Server.Mappath(DBFileName) &";Driver={Microsoft Access Driver (*.mdb)};"

'-- default SQL
' Change the SQL string to a SQL string for your DB
SQLstring = "SELECT * FROM Customers where CustomerID;"

myRecordSet.Open SQLstring, DataConnection
%>

<body>
<%
' Displaying First row of SQL result in browser window, if all rows should be displayed you can iterate through the results with a do while loop around the following three lines of code.
Response.write(myRecordSet.Fields("CustomerID") & "<BR>")
Response.write(myRecordSet.Fields("CustF_Name") & "<BR>")
Response.write(myRecordSet.Fields("CustL_Name") & "<BR>")
%>

</body>

<%
' closing objects and setting them to nothing
' not neccesary but a good habit
myRecordSet.Close
Set myRecordSet = Nothing
DataConnection.Close
Set DataConnection = Nothing
%>

</html>