nathan schmoll's
aspdiscovery

back to main page

e-mail me








DSN-less Database Connections!
OK, if you wanted to connect to a database in an ASP script, you'd probably connect to it using a Data Source Name (DSN), right? Why would you do that? You can open up and work with any Access database by just opening it up! That's right. There is a way to get the script to open up an Access database directly from the code. And it's actually fairly simple, too! Here's how.

Okay, we start off by mapping a path to the Access database. We'll use a database file called "salesstuff".

<%
Set MyConn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("salesstuff.mdb")
MyConn.Open "Driver={Microsoft Access Driver _
(*.mdb)}; DBQ=" & MdbFilePath & ";"
%>

Then we tell the script what to select and from which table to select it from. In this example, we'll just select all the records in a table called "sales".

<%
SQL_query = "SELECT * FROM sales"
%>

You're done! All you have to do is use regular SQL commands. Now you can easily query a database without having to make a DSN to the database and bugging the heck out of the technical support people at your hosting company!

copyright © 2001, nathan schmoll. all rights reserved. questions? comments? suggestions? e-mail me!