Connect to the database
All the pieces are in place. Now you’ll use the System DSN you created to connect to the Northwind.mdb sample database in \inetpub\wwwroot\sample the same directory where you stored the ASP example scripts. Open a browser and execute each ASP file by entering a URL in the form http://server/subfoldername/filename.asp where server is the name of your server, subfoldername identifies the folder within wwwroot containing the ASP file, and filename.asp is the name of the ASP file that you are executing. In this case, the server should be localhost, so to run the first example file enter http://localhost/sample/example.asp
The result should be a list of names, last then first, of each employee in the Northwind Employees table. Let’s look at the actual scripts; all of the ASP code is enclosed in < % and %> tags. In example.asp, the first statement< %Set Conn = Server.CreateObject("ADODB.Connection") Creates a new instance of the connection object named Conn. You can give objects any name that you want, but descriptive names are recommended. By default the connection is read-only, which is what we want in this case. You can change this by adding a following line Conn.Mode = constant Where constant is adModeRead for read-only, adModeWrite for write-only, or adModeReadWrite to allow read and write.
Now you must open the connection using the System DSN that you created earlier. So the next statement Conn.Open "NorthWindDSN" Points the connection to the NorthwindDSN, which gives you access to the Northwind sample database in \inetpub\wwwroot\sample.The next statements
Sql = "SELECT LastName, FirstName FROM Employees"
set rst = Conn.Execute(sql)
Populate rst with the data from the LastName and FirstName fields in the Employees table. The While loop then moves through each record in rst, displaying the contents of the LastName and FirstName fields.
This simple example returns two data fields from every record in the table. You will rarely retrieve so much data and will more likely limit the retrieved information to some criteria. To illustrate, example2.asp restricts the SQL statement to retrieve only those records where the LastName field equals the string ‘Reid’:
Sql = "SELECT LastName, FirstName FROM employees WHERE LastName = ‘Reid’"
The If statement checks rst for records, and the EOF property will be true if rst is empty. In which case, the code displays an error message, and Response.End Kills the script. Any ASP or HTML after this statement will not be executed.
The first two ASP files deal with simple data retrieval. The third grabs the data from the LastName and FirstName fields in the Employees table and format it in a simple HTML table. The formatting begins with
<caption align=’"top"’>Employee List from Northwind. Demo</caption>
This starts the table and adds a title above it, while the statements
Add headings to the table columns. The <tr></tr> and <td></td> tags create table rows and cells while the While loop populates it. The bgcolor="#00FFFF" attribute adds a bit of color to each row.
Click Next for modify-data-online page.