• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 27. Building a Basic ASP Site > Setting Up a Database Connection

Setting Up a Database Connection

In ASP terms, the database connection is a script that calls on the driver to talk to the database. Dreamweaver creates this script for you and stores it in a special connections file when you choose data source name (DSN) from the Databases panel. Because this information gets stored in a special file that can be accessed by any ASP page in your site, you have to define the connection only once for the entire site.

Exercise 27.2 Creating a Database Connection

In this exercise, you create the connection script that will allow your pages to communicate with the antiques database. You must already have installed your database and created a driver for it (as outlined in the preceding section) before continuing with this exercise.

Because Dreamweaver has to know what kind of connection to create, you must have a dynamic document open before you can create the connection. From your local site, open catalog.asp.

From the Application panel group, open the Databases panel. If you have catalog.asp open, the panel will have a plus (+) button at the top. Click it and, from the pop-up menu, choose data source name (DSN). Figure 27.6 shows this happening.

Figure 27.6. Choosing a DSN from the Databases panel.

The Data Source Name dialog box will appear. For your connection’s name, enter antiques_conn. (The connection name will be used in the connection script. It can be any one-word name with no special characters, but it’s common practice to include con or conn in the name.)

If your server is on your working computer, the radio buttons at the bottom of the dialog box should be set to Using Local DSN, and there will be a pop-up list of drivers. Choose AntiquesBarn from the list. (Clicking the Define button will open the ODBC control panel.) If your server is on another computer, the radio buttons should be set to Using DSN on Testing Server, and the dialog box will include a DSN button. Click that button to get a list of DSNs and choose AntiquesBarn from that list. Figure 27.7 shows both of these possibilities.

Figure 27.7. Defining the antiques_conn DSN on a computer housing a web server (Windows) and on a computer networked to a server (Mac).


Occasionally, Mac users will not be able to get a pop-up list of drivers. If this happens to you, carefully type in the name of the driver in the DSN input field. As long as the name is exactly correct, and you have set up a successful link to the host computer in the Site Definition dialog box, this will work.

If you specified a name and password when you defined the driver, enter them here. Otherwise, you can leave these fields blank.

Before leaving the Data Source Name dialog box, click the Test button. If Dreamweaver can find the driver, you’ll get a Connection Successful message. The most common reasons for failing the test are incorrect names and passwords, and incorrectly named DSNs. (If you chose the name from a pop-up menu, this won’t be an issue.) After you’ve passed the test, click OK to close the dialog box.

The Databases panel will now contain an icon representing your connection. (Congratulations!) You can now use this panel to explore your database. Expand the connection icon to see Tables, Views, and Stored Procedures. The antiques database contains only tables. Expand the Tables icon all the way to see that the database contains two tables—stockitems and customers—and to see what columns (information fields) each table contains. You cannot see the records stored in the database from here, but you can examine its structure (see Figure 27.8).

Figure 27.8. The Databases panel showing the structure of the antiques database.

In the Site panel, examine your local root folder. You’ll see a new connections folder. Inside that folder is the antique_conn.asp file. That file contains your connection script. Open that file and examine it in Code view. You’ll see the following connection script:
// FileName="Connection_odbc_conn_dsn.htm" 
// Type="ADO" 
// DesigntimeType="ADO" 
// HTTP="false" 
// Catalog="" 
// Schema="" 
var MM_antique_conn_STRING = "dsn=AntiqueBarn;" 

You don’t need to know what everything in there means. However, one important piece of syntax that you should get familiar with is the <%...%> tags. All ASP code in an HTML document is contained within these tags. Whatever code is inside the tags must be valid JavaScript (or VBScript, if that’s the language you chose when setting up). When the application server processes this page, it looks for these tags and executes all code inside them. All other code on the page is assumed to be regular HTML or client-side scripting and is just passed back to the browser.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint