ASP Database Connection

ASP is a great tool for building dynamic web pages.  One of the most basic scripts you need in your ASP web pages is the database connection script.  Here are two examples of database connections.

This example uses a DSN connection:

<%
DIM objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "DSN=myCONNECTION.dsn"
objConn.Open
%>

TIP – You will need to setup your DSN with either your hosting company or your administrator if you have your own server.

This example uses a DSN-less connection:

<%
DIM objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath ("/mydatabase.mdb") & ";"
objConn.Open
%>

When you copy / paste the DSN-less code to your page, just remove the _ at the end of the objConn.ConnectionString line and put the Server.MapPath on the same line.  As a rule of thumb, this should always go on one line, but for display purposes here we had to put it on two lines.

If you want to learn more about these connection methods, be sure to read our Open Database Connection Tutorial.

< Back to ASP Scripts