MS Access


How do I connect to MSAccess using ASP?
Connection String Samples
There are many ways to connect to a database. This KB shows the requirements for connecting to a database, how to setup a DSN, and sample connection strings for various types of databases including Microsoft Access and Microsoft SQL Server and database connections.

Microsoft Access and SQL 2000 Databases

With DSN and no User ID/Password

<%
Set connectionToDatabase=Server.CreateObject("ADODB.Connection")
connectionToDatabase.ConnectionTimeout=60
connectionToDatabase.Open"DSN=DSNname"
%>


VERY IMPORTANT! Make sure to insert the following line where you want to
close the connection]


<%
connectionToDatabase.Close
Set connectionToDatabase=Nothing
%>


Without DSN

<%
Set Cnn = Server.CreateObject("ADODB.Connection")
Cnn.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\domains\yourdomain.com\db\mydatabase.mdb"
%
>


OLE DB

<%
Set Cnn = Server.CreateObject("ADODB.Connection")
Cnn.open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=c:\domains\yourdomain.com\db\mydatabase.mdb"
%>


File DSN

<%
Set Cnn = Server.CreateObject("ADODB.Connection")
Cnn.open "FILEDSN=DSNname"
%>


Another Example of With DSN and no User ID/Password

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open "DSN=DSNname"
%>


With DSN and User ID/Password

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open "DSN=DSNname","username","password"
%>


Without DSN, using a physical path as a reference

<%
Set Conn = Server.CreateObject("ADODB.Connection")
DSNtest="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtest=dsntest & "DBQ=c:\domains\yourdomain.com\db\mydatabase.mdb"
Conn.Open DSNtest
%>


Without DSN, using Server.MapPath

NOTE: Server.MapPath is the path from the Web server root. By default, this is C:\domains\yourdomains.com.

<%
Set Conn = Server.CreateObject("ADODB.Connection")
DSNtest="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtest=dsntest & "DBQ=" & Server.MapPath("/db/mydatabase.mdb")
Conn.Open DSNtest
%>

Microsoft SQL Server 2000 Connection Strings

With DSN

<%
dbconn="Provider=SQLOLEDB.1;UID=user;Password=password;Initial Catalog=DSNname;Data Source=IPaddressoftheSQLserver,PortNumber
" %>

OLE DB

<%
Set cnn = Server.CreateObject("ADODB.Connection")
cnn.open "PROVIDER=SQLOLEDB;DATA SOURCE=sqlservername; UID=username;PWD=password;DATABASE=mydatabasename "
%>


With DSN

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open "DSN=DSNname;UID=user;PWD=password;DATABASE=mydatabasename"
%>


Without DSN

<%
Set Conn = Server.CreateObject("ADODB.Connection")
DSNtest="DRIVER={SQL Server};SERVER=ServerName; UID=USER;PWD=password;DATABASE=mydatabasename"
Conn.open DSNtest
%>

NOTE: If you do not want to put your SQL 2000 connection string on your actual ASP pages where the public can view the source and see it, you can create an include file from a hidden page and then call it from within your page where desired. This is a common practice and often used to protect your SQL database.

mySQL Connection Strings

Without DSN

<%
dbconn="Driver={MySQL ODBC 3.51 Driver};UID=user;Password=password; Database=mydatabasename;SERVER=IPaddressoftheSQLserver
" %>