HOW TO: Connect to a Database by Using Active Server Pages in Windows 2000 (313062)
The information in this article applies to:
- Microsoft Windows 2000 Server
- Microsoft Windows 2000 Advanced Server
- Microsoft Windows 2000 Professional
- Microsoft Small Business Server 2000
- Microsoft Internet Information Services 5.0
This article was previously published under Q313062 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):
- Log on to the Web server computer as
administrator.
- Click Start, point to Settings, and then click Control Panel.
- Double-click Administrative Tools, and then double-click Data Sources (ODBC).
- Click the System DSN tab, and then click Add.
- Select the database driver that you want (for example,
Microsoft Access Driver (*.mdb), and then click Finish.
- 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.
- In the Description box, type an optional description for the DSN. For example,
Northwind DSN.
- Click Select.
- 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. - Click OK.
- Click Advanced.
- 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.
- Click OK, and then click OK.
back to the top
For a Database Server To create a DSN for an SQL Server:
- Log on to the Web server computer as
administrator.
- Click Start, point to Settings, and then click Control Panel.
- Double-click Administrative Tools, and then double-click Data Sources (ODBC).
- Click the System DSN tab, and then click Add.
- Select SQL Server, and then click Finish.
- 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.
- In the Description box, type an optional description for the DSN.
- In the Server list, do one of the following:
- Select the name of an SQL Server on the
network.
- Select (local) if the SQL Server that you want is running on the Web server
computer.
- Type a name (alias) for a server that does not appear
in the Server list.
- Click Next.
- 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.
- 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.
- 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:
<HTML>
<HEAD><TITLE>ASP Database Connection</TITLE></HEAD>
<BODY BGCOLOR=white>
<H1>Northwind Database Contacts</H1>
<%
Dim Connect, selectSQL, RecSet
Set Connect = CreateObject ("ADODB.Connection")
Connect.Open "DSN=Northwind"
selectSQL = "SELECT * FROM Customers"
Set RecSet = Connect.Execute (selectSQL)
If NOT RecSet.EOF
THEN
DO UNTIL RecSet.EOF
Response.Write RecSet("Companyname") & ", " & RecSet("Contactname") & "<BR><BR>"
RecSet.MoveNext
Loop
End If
RecSet.Close
Connect.Close
Set RecSet = Nothing
Set Connect = Nothing
%>
</BODY></HTML>
- On the File menu, click Save As.
- 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.
- Quit Notepad.
back to the top
Step 3: Test the ASP Page- Click Start, and then click Run.
- 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
REFERENCESFor additional information about how to work with
ASP pages, click the article numbers below to view the articles in the
Microsoft Knowledge Base: 308164 HOW TO: Create and Configure ASP Web Applications
165492 HOWTO: Use ADO with a Visual Foxpro Database
For additional information about SQL Server, please
view the following Microsoft Web site: For additional information about Microsoft Scripting
technologies, please view the following Microsoft Web sites: For additional information about ODBC, please view the following
Microsoft Web site:
back to the top
Modification Type: | Major | Last Reviewed: | 4/19/2006 |
---|
Keywords: | kbenv kbhowto kbHOWTOmaster KB313062 kbAudITPro |
---|
|