To 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 that you want to connect to. To do this, use one of the following methods. To create a data source name for a database program To create a DSN for a database program, such as Microsoft Access, follow these steps: 1. Log on to the WEB server as an administrator. 2. ClickStartPointingSet up, and then clickControl Panel。 3. Double clickManagement Tools, and then double-clickdata Source (ODBC)。 4. Clicksystem DSNtab, and then clickAdd。 5. Select the desired database driver (for example, "Microsoft Access Driver (*.mdb) [/b]"), and then clickComplete。 6. InData Source Namebox, type the name that you want to use when referencing this DSN in ASP code. For example, Northwind. 7. InDescriptionbox, type an optional description for the DSN. For example, the Northwind DSN. 8. ClickSelect。 9. InSelect Databasedialog box, browse to and select the database that you want. For exampleNorthwind.mdb。 Notes: If the database is not on the WEB server, clickNetwork, and then clickBrowse。 Locate the shared network folder that contains the database, and then clickDetermine。 ClickComplete, and then select the database that you want. 10. ClickDetermine。 11. ClickAdvanced。 12. If you want to automatically provide logon credentials to the database when you use this DSN, type them into the login name [/b] andPasswordbox. ClickDetermine。 13. ClickDetermine, and then clickDetermine。 to create a data source name for the database server To create a DSN for SQL Server, follow these steps: 1. Log on to the WEB server as an administrator. 2. ClickStartPointingSet up, and then clickControl Panel。 3. Double clickManagement Tools, and then double-clickdata Source (ODBC)。 4. Clicksystem DSNtab, and then clickAdd。 5. SelectSQL Server, and then clickComplete。 6. Innamebox, type the name that you want to use when referencing this DSN in ASP code. For example, Northwind. 7. InDescriptionbox, type an optional description for the DSN. 8. InServerList, do one of the following: • Select the name of SQL Server on the network. • If the SQL Server you want is running on a WEB server, select(local)。 • For not appearing inServerThe server in the list type a name (alias). 9. ClickNext。 10. Under how SQL Server should verify the authenticity of the logon ID [/b], click the authentication method that you want. For example, "Use Windows NT authentication [/b] with a network logon ID." ClickNext。 11. Click to select the change the default database to [/b] check box to select the desired database from the list (for example,Northwind), and then clickNext。 12. ClickCompleteClickDetermine, and then clickDetermine。 Create an ASP script to connect to a database using DSN In ASP scripts, a connection to a database is created by using the Database access component (DAC) of the ActiveX data Object (ADO): • Create a connection to the database using the Connection ADO object. • Retrieve, update, and delete existing database records using the Recordset object. Sample The following example illustrates how to connect to the Microsoft Access NorthWind sample database by using ASP script. Notes: This example assumes that Windows 2000, Microsoft Access, and the Northwind sample database are installed by default on drive C. Step 1th: Create a DSN 1. Log on to the WEB server as an administrator. 2. ClickStartPointingSet up, and then clickControl Panel。 3. Double clickManagement Tools, and then double-clickdata Source (ODBC)。 4. Clicksystem DSNtab, and then clickAdd。 5. Select Microsoft Access Driver (*.mdb) [/b], and then clickComplete。 6. InData Source Namebox, type Northwind, and then clickSelect。 7. InSelect Databasedialog box, browse to the location of the Northwind.mdb database, and then click it. By default, the Northwind.mdb file is located in the C:/Program Files/microsoft office/office/samples folder. 8. ClickDetermineClickDetermine, and then clickDetermine。 Step 2nd: Create an ASP page 1. Start "Notepad". 2. In Notepad, type the following code: <HTML> <body bgcolor=white> <% 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> 3. Infilemenu, clickSave As。 4. InSave Asdialog box, browse to the "saved in/b" listC:/inetpub/wwwroot, in the Save as type [/b] list, selectAll Files, type database.asp in the file name [/b] box, and then clickSave。 5. Quit "Notepad". 3rd Step: Test ASP page 1. ClickStart, and then clickRun。 2. InOpenbox, type http://localhost/database.asp, and then clickDetermine。 A Web page appears in the browser window that displays a list of customers for the NorthWind sample database. Troubleshoot If you experience problems connecting to a database by using an ASP Web page, verify that you have sufficient permissions to access the database: • Verify that the account used by the DSN has sufficient permissions to access the database. • If you are trying to connect to SQL Server over a network, make sure that you use the domain account as an anonymous IIS account. • If you are trying to connect to a Microsoft Access database, make sure that the IIS account has write access to the folder where the database is stored. This is to allow users to create temporary files when they access the database. • Make sure that you are creating a system DSN. ActiveX Data Objects (ADO) does not recognize a user (or local) DSN. Because the System DSN stores the settings in the registry, and the file DSN stores the connection parameters in the files on the hard disk, the System DSN executes slightly faster than the file DSN. |
|