Microsoft KB Archive/313062

= HOW TO: Connect to a Database by Using Active Server Pages in Windows 2000 =

Article ID: 313062

Article Last Modified on 11/1/2006

-

APPLIES TO


 * Microsoft Windows 2000 Server
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Professional Edition
 * Microsoft Small Business Server 2000 Standard Edition
 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q313062



IN THIS TASK
SUMMARY Create a Data Source Name
 * For a Database Program
 * For a Database Server

Create an ASP Script to Connect to the Database by Using the DSN Example
 * Step 1: Create a DSN
 * Step 2: Create an ASP Page
 * Test the ASP Page

Troubleshooting REFERENCES



SUMMARY
This step-by-step article describes how to connect to a database by using an Active Server Pages (ASP) Web page.

back to the top

Create a Data Source Name
To connect to a database by using ASP pages, you must first create a Data Source Name (DSN) on the Web server for the type of database to which you want to connect. To do so, use one of the following methods.

back to the top

For a Database Program
To create a DSN for a database program (such as Microsoft Access):
 * 1) Log on to the Web server computer as administrator.
 * 2) Click Start, point to Settings, and then click Control Panel.
 * 3) Double-click Administrative Tools, and then double-click Data Sources (ODBC).
 * 4) Click the System DSN tab, and then click Add.
 * 5) Select the database driver that you want (for example, Microsoft Access Driver (*.mdb), and then click Finish.
 * 6) In the Data Source Name box, type the name that you want to use when you refer to this DSN in your ASP code. For example, Northwind.
 * 7) In the Description box, type an optional description for the DSN. For example, Northwind DSN.
 * 8) Click Select.
 * 9) In the Select Database dialog box, browse to and select the database that you want. For example, Northwind.mdb.

NOTE: If the database is not on the Web server, click Network, and then click Browse. Locate the shared network folder that contains the database, and then click OK. Click Finish, and then select the database that you want.
 * 1) Click OK.
 * 2) Click Advanced.
 * 3) If you want to automatically provide login credentials to the database when you use this DSN, type them into the Login name and Password boxes. Click OK.
 * 4) Click OK, and then click OK.

back to the top

For a Database Server
To create a DSN for an SQL Server:
 * 1) Log on to the Web server computer as administrator.
 * 2) Click Start, point to Settings, and then click Control Panel.
 * 3) Double-click Administrative Tools, and then double-click Data Sources (ODBC).
 * 4) Click the System DSN tab, and then click Add.
 * 5) Select SQL Server, and then click Finish.
 * 6) In the Name box, type the name that you want to use when you refer to this DSN in your ASP code. For example, Northwind.
 * 7) In the Description box, type an optional description for the DSN.
 * 8) In the Server list, do one of the following:
 * 9) * Select the name of an SQL Server on the network.
 * 10) * Select (local) if the SQL Server that you want is running on the Web server computer.
 * 11) * Type a name (alias) for a server that does not appear in the Server list.
 * 12) Click Next.
 * 13) Under How should SQL Server verify the authenticity of the login ID, click the authentication method that you want. For example, With Windows NT authentication using the network login ID. Click Next.
 * 14) Click to select the Change the default database to check box, select the database that you want from the list (for example, Northwind), and then click Next.
 * 15) Click Finish, click OK, and then click OK.

back to the top

Create an ASP Script to Connect to the Database by Using the DSN
In your ASP script, create a connection to the database by using the ActiveX Data Objects (ADO) Database Access Component (DAC):
 * Use the Connection ADO object to create a connection to the database.
 * Use the Recordset object to retrieve, update, and delete existing database records.

back to the top

Example
The following example illustrates how to connect to the Microsoft Access NorthWind sample database by using an ASP script.

NOTE: This example assumes the default installation of Windows 2000 on drive C, as well as the default installation of Microsoft Access along with the NorthWind sample database.

back to the top

Step 1: Create a DSN
Follow the steps in the For a Database Program section of this article to create a DSN to the Microsoft Access Northwind.mdb sample database.

NOTE: By default, the Northwind.mdb file is located in the C:\Program Files\Microsoft Office\Office\Samples folder.

back to the top

Step 2: Create an ASP Page
 Start Notepad.  In Notepad, type the following code:  ASP Database Connection  Northwind Database Contacts <% Dim Connect, selectSQL, RecSet Set Connect = CreateObject (&quot;ADODB.Connection&quot;) Connect.Open &quot;DSN=Northwind&quot; selectSQL = &quot;SELECT * FROM Customers&quot; Set RecSet = Connect.Execute (selectSQL) If NOT RecSet.EOF THEN DO UNTIL RecSet.EOF Response.Write RecSet(&quot;Companyname&quot;) & &quot;, &quot; & RecSet(&quot;Contactname&quot;) & &quot;<BR>&quot; RecSet.MoveNext Loop End If RecSet.Close Connect.Close Set RecSet = Nothing Set Connect = Nothing %> </BODY></HTML> </li> On the File menu, click Save As.</li> In the Save As dialog box, navigate to C:\Inetpub\wwwroot in the Save in list, select All Files in the Save as type list, type database.asp in the File name box, and then click Save.</li> Quit Notepad.</li></ol>

back to the top

Step 3: Test the ASP Page

 * 1) Click Start, and then click Run.
 * 2) In the Open box, type http://localhost/database.asp, and then click OK. A Web page that displays the NorthWind sample database customer list is displayed in the browser window.

back to the top

Troubleshooting
If you experience difficulty connecting to a database by using ASP Web pages, verify that you have sufficient permissions to access the database:
 * Verify that the DSN is using an account with sufficient permissions to access the database.
 * If you attempt to connect to a SQL Server over a network, ensure that you use a domain account as your anonymous IIS account.
 * If you attempt to connect to a Microsoft Access database, ensure that the IIS account has Write permissions to the folder in which the database is stored. This is to enable the creation of a temporary file when users access the database.

back to the top

<div class="references_section">