- Jun 28, 2002
Setting Up an ODBC Connection
For your applications to know where the database file is and how to talk to it, you will use Open Database Connectivity (or ODBC). ODBC usually comes with several database drivers already installed, such as Access, SQL Server, dBase, Excel, FoxPro, Oracle, text. Each of these drivers knows the characteristics of its respective datatype and communicates your SQL commands to it. This way, you don't have to know exactly how to talk to each individual datatype. This is the beauty of SQL!
After you have a data file saved to an appropriate spot on your machine or on a shared drive on your network, you need to set up the ODBC connection. There are a couple of ways in which to do this.
Windows Control Panel
Open the control panel from the settings menu and choose the ODBC control panel. There are several tabs you can choose from, but we will be paying attention to the tab System DSN (Data Source Name).
FIGURE 16 The System DSN tab of the ODBC control panel.
Now you need to locate the data file and establish an ODBC connection by clicking the Add button, which brings you to a list of several types of database drivers to choose from. Select the Microsoft Access Driver, and click Finish.
FIGURE 17 This is where you select the Microsoft Access driver.
Now you will tell the system where the Access database is located by clicking the Select button and locating the file. Next give the file a name. For our example, we will name it shelleyCatalog. Don't use spaces for the name. You can use underscores for clarity, if needed. (In this example, you might name this file shelley_Catalog.) Now click OK.
FIGURE 18 Naming the ODBC connection, and defining the location of the datasource.
Now you have successfully set up your ODBC connection to the shelleyCatalog.mdb file, which you will work with later in this book.
FIGURE 19 The ODBC connection is now set up.
The better and far easier way to set up an ODBC connection is through the ColdFusion server's built-in administration screen. If you are using an ISP to host your web site, chances are good that you cannot use this and will have to have the ISP set it up for you.
ColdFusion Server has a built-in administration interface that allows you to control many features of your server either locally or remotely. To access this on your local machine type in the following URL:
You will be asked to enter a password to log into it. This is the password you entered when you originally set up ColdFusion Server.
FIGURE 110 ColdFusion Server administrator.
Under the Data Sources heading, select ODBC. This lists all the ODBC connections currently running on your machine. It tells you the name of the file and the ODBC driver it is using, and verifies the connection. If you have made a change to a file and want to make sure the DSN connection is still intact, click the Verify link to have the application check it for you. Alternatively, you can select the Verify All button at the top to have all of your DSNs verified.
Now you want to set up your ODBC connection (assuming you haven't already done so using the Windows ODBC control method). Select ODBC under the DATA SOURCES section, and you will see the list of your current ODBC connections. Select the ADD button with the appropriate database type selected in the drop-down menu. Then add the database name and the location of the database you will be using.
FIGURE 111 ColdFusion's ODBC administration screen.
Different database drivers ask for different types of information, but they all ask for three common pieces of information:
Database name: This doesn't necessarily have to be the name of the database file, but it is the name you will be using for that connection.
Description: This is used only for reference. When you are running several types or different versions of a database, this is a good way to remember which is which.
Database File or Server: Using the Browse Server button, you can locate the file on your hard drive or network. Using SQL server, you will have to type the location of the machine on your network. If this database file is on the same machine, just type (local) and enter the database name in the Database field next to Login Info.
The Browse Server is a Java class application that operates fine under Netscape. However, under Internet Explorer (all versions), it may ask you to connect to the Microsoft web site to download the necessary Java class files.
If you have set any password restrictions on a database, be sure to enter those restrictions in this area as well. SQL server connections select the CF Settings to enter this information.
After you have created the ODBC connection, go back to your list of ODBC connections and your new connection will be listed. You are now ready to start learning some ColdFusion commands.
FIGURE 112 Verification that the ODBC connection was set up correctly.