Microsoft KB Archive/300382

= How To Create a Database Connection from an ASP Page in IIS =

Article ID: 300382

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft Internet Information Services 5.0, when used with:
 * Microsoft Windows 2000 Standard Edition

-



This article was previously published under Q300382



IN THIS TASK

 * SUMMARY
 * Requirements
 * Sample Database Connection Strings
 * Microsoft Access
 * Microsoft SQL Server
 * Microsoft Visual FoxPro
 * Oracle
 * REFERENCES



SUMMARY
There are many ways to connect to a database. This step-by-step article provides sample connection strings for various types of databases and database connections.

back to the top

Requirements
The following are the requirements for connecting to a database:
 * Active Server Pages (ASP) enabled Internet Information Services (IIS) version 5.0 Web server with Microsoft Data Access Components (MDAC) version 2.5 or 2.6 (with a Jet database engine)
 * Connectivity to a local or remote database
 * ASP enabled Microsoft Internet Explorer version 5.0 or later

back to the top

Sample Database Connection Strings
These examples are for demonstration purposes only. You must paste this code in your ASP code to make a connection to the specified database. Note that you must change elements such as database name, server name, database location, Data Source Name (DSN), and so on.

back to the top

Microsoft Access
Without DSN <% Set Cnn = Server.CreateObject(&quot;ADODB.Connection&quot;) Cnn.open &quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\mydatabase.mdb&quot; %> OLE DB <% Set Cnn = Server.CreateObject(&quot;ADODB.Connection&quot;) Cnn.open &quot;PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=c:\mydatabase.mdb&quot; %> File DSN <% Set Cnn = Server.CreateObject(&quot;ADODB.Connection&quot;) Cnn.open &quot;FILEDSN=ADSN&quot; %> With DSN and no User ID/Password <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) Conn.open &quot;DSNname&quot; %> With DSN and User ID/Password <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) Conn.open &quot;DSNname&quot;,&quot;username&quot;,&quot;password&quot; %> Without DSN, using a physical path as a reference <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) DSNtest=&quot;DRIVER={Microsoft Access Driver (*.mdb)}; &quot; DSNtest=dsntest & &quot;DBQ=c:\mydatabase.mdb&quot; Conn.Open DSNtest %> Without DSN, using Server.MapPath

NOTE: Server.MapPath is the path from the Web server root. By default, this is C:\Inetpub\Wwwroot. <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) DSNtest=&quot;DRIVER={Microsoft Access Driver (*.mdb)}; &quot; DSNtest=dsntest & &quot;DBQ=&quot; & Server.MapPath(&quot;/databases/mydatabase.mdb&quot;) Conn.Open DSNtest %> back to the top

Microsoft SQL Server
OLE DB <% Set cnn = Server.CreateObject(&quot;ADODB.Connection&quot;) cnn.open &quot;PROVIDER=SQLOLEDB;DATA SOURCE=sqlservername;UID=username;PWD=password;DATABASE=mydatabase &quot; %> With DSN <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) Conn.open &quot;DSN=MyDSN;UID=user;PWD=password;DATABASE=mydatabase&quot; %> Without DSN <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) DSNtest=&quot;DRIVER={SQL Server};SERVER=ServerName;UID=USER;PWD=password;DATABASE=mydatabase&quot; Conn.open DSNtest %> back to the top

Microsoft Visual FoxPro
Without DSN <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) ConnStr= &quot;Driver=Microsoft Visual Foxpro Driver; UID=userID;SourceType=DBC;SourceDB=C:\databases\mydatabase.dbc&quot; Conn.Open ConnStr %> back to the top

Oracle
ODBC with DSN <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) Conn.cursorlocation=adUseClient ' requires use of adovbs.inc; numeric value is 3 Conn.open &quot;DSN=test;UID=name;PWD=pass&quot; %> OLE DB <% Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;) Conn.cursorlocation=adUseClient ' requires use of adovbs.inc; numeric value is 3 DSNTest=&quot;Provider=MSDAORA.1;Password=pass;User ID=name;Data Source=data.world&quot; Conn.open DSNtest %> back to the top

