SQL Server Database Access With IIS
SQL Server Database Access With IIS
April 2001
Creating a DSN
Developers most commonly use ADO with a DSN in ASP files. A DSN is the logical name that ODBC uses to refer to the required database information. IIS uses a DSN to connect to an ODBC data source, such as the PUBS SQL Server database. You must create a DSN on your IIS Web server machine that the ASP file we provide (GetTitles.asp) can use to access data from the PUBS database on your SQL Server machine (which will most likely be on a different machine). To create a DSN that connects to your SQL Server PUBS database, in Windows NT, access the Control Panel ODBC Data Sources applet; in Windows 2000, access the Control Panel Administrative Tools applet, then double-click Data Sources (odbc). You're going to create a system DSN, including the services (e.g., IIS) running as a part of the OS, that will be available to all users on the machine. On the ODBC Data Source Administrator dialog box, click the System DSN tab, which Figure 1 shows, then click Add. To create a new data source, you must first identify the type of data to which you want to connect. In this case, it's SQL Server, so scroll to the bottom of the driver list and select that option, as Figure 2 shows. Click Finish to proceed with the New Data Source to SQL Server Wizard. Now, name your DSN pubs. (Naming your DSN after the SQL Server database it's attaching to alleviates confusion on IIS machines that have numerous DSNs with connectivity to numerous SQL Server machines.) Next, add a description of the data source. We chose the name System DSN for the PUBS database, as Figure 3 shows. Finally, chose the name of the SQL Server that you're attaching to. (You might have to type its name.) Click Next. Now, you choose the authentication credentials for the system DSN that you're creating. The default choice, With Windows NT authentication using the network login ID, uses the credentials of the currently authenticated user. For this example, let's select the With SQL Server authentication using a login ID and password entered by the user option, then type a valid SQL Server login ID and password that have sufficient privileges to read from the PUBS database, as
Figure 4, page 11, shows. For this example, we used the all-powerful sa account for the system DSN to authenticate with. Click Next. Select pubs from the Change the default database to drop-down list, as Figure 5 shows. (Notice that you already have connectivity to the SQL Server database because the wizard has listed all the databases on the SQL Server you've attached to.) Leave the rest of the default choices, then click Next twice. A summary page like the one Figure 6 shows will appear. Click Test Data Source to test connectivity to the SQL Server database.
This article has demonstrated how to use ADO with a DSN to use SQL Server data in your ASP files. Keep in mind that we discussed and used only a small part of the feature-rich functionality of ADO. In future articles, we'll show you how IIS can leverage ADO to access other data sources, such as Exchange Server and AD. In addition, we'll show you how to use ADO without a DSN and explain why sometimes that option is better.
Tip: Click Client Configuration, and make sure that your DSN is using TCP/IP as the network library. TCP/IP provides a reliable and fast access to SQL Server. Note: Using an administrator account in the open string is bad practice because someone could read the source code and gain access to the SQL Server machine. Instead, use an account that can only access the necessary tables and stored procedures in the specific database (in this case, PUBS).
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5
Figure 6
Figure 7
Listing 1
Listing 1: Excerpt from adovbs.inc '---Const Const Const Const '---Const Const Const Const Const Const Const Const CursorTypeEnum Values ---adOpenForwardOnly = 0 adOpenKeyset = 1 adOpenDynamic = 2 adOpenStatic = 3 CursorOptionEnum Values ---adHoldRecords = &H00000100 adMovePrevious = &H00000200 adAddNew = &H01000400 adDelete = &H01000800 adUpdate = &H01008000 adBookmark = &H00002000 adApproxPosition = &H00004000 adUpdateBatch = &H00010000
Const Const Const Const Const '---Const Const Const Const '---Const Const Const Const Const
adResync = &H00020000 adNotify = &H00040000 adFind = &H00080000 adSeek = &H00400000 adIndex = &H00800000 LockTypeEnum Values ---adLockReadOnly = 1 adLockPessimistic = 2 adLockOptimistic = 3 adLockBatchOptimistic = 4 ExecuteOptionEnum Values ---adAsyncExecute = &H00000010 adAsyncFetch = &H00000020 adAsyncFetchNonBlocking = &H00000040 adExecuteNoRecords = &H00000080 adExecuteStream = &H00000400
'---- ConnectOptionEnum Values ---Const adAsyncConnect = &H00000010 '---Const Const Const Const Const ObjectStateEnum Values ---adStateClosed = &H00000000 adStateOpen = &H00000001 adStateConnecting = &H00000002 adStateExecuting = &H00000004 adStateFetching = &H00000008
'---- CursorLocationEnum Values ---Const adUseServer = 2 Const adUseClient = 3 '---Const Const Const Const Const Const Const Const Const Const Const DataTypeEnum Values ---adEmpty = 0 adTinyInt = 16 adSmallInt = 2 adInteger = 3 adBigInt = 20 adUnsignedTinyInt = 17 adUnsignedSmallInt = 18 adUnsignedInt = 19 adUnsignedBigInt = 21 adSingle = 4 adDouble = 5