(Ebook - PDF) - ASP Data Access For Beginners - SQL
(Ebook - PDF) - ASP Data Access For Beginners - SQL
By Jason Butler
Introduction
The driving force behind Active Server technologies is data access. In order to develop a truly dynamic web
site developers need to allow users to access data on demand. Luckily, with Microsoft's Active Server
Pages, this is exceedingly easy to do. This article for ASP beginners details how to connect to a SQL Server
7.0 database using ActiveX Data Object (ADO) and Open Database Connectivity (ODBC).
In order to illustrate the process of connecting to a data source with ASP, we will need to do three things:
? Create a database
? Create an ODBC data source name (DSN)
? Create an ASP page
First we need a database. Since this article isn't about database design, we will create a very simple SQL
Server database -- one table! We'll name this database 15Seconds, and we will name our table t_articles.
1. Open SQL Server 7.0 Enterprise Manager (Start ( Programs (SQL Server 7.0 (Enterprise
Manager).
2. Expand the Enterprise Manager tree, selecting the SQL Server to which you would like to add the
database, until you see the "Database" node.
Figure 1
3. Right click on the "Database" node and select "New Database. . ."
4. On the "Database Properties" dialog box, enter "15Seconds" in the "Name" field.
Figure 2
We now have a database named "15Seconds" to which we can add our table.
Now we have a database and table. Let's add some sample data. We'll add one record. To add data to
t_articles, perform the following:
Figure 4
So, there is the entire database. Pretty impressive, huh? Now that we have a database and a table, we need to
create an ODBC connection to our database.
Figure 5
6. On the "Create a New Data Source to SQL Server" dialog box (see Figure 6):
•Enter "15Seconds" in the "Name" field. This is not the name of the database, but the name for
the DSN. I kept it the same just for simplicity, however, this is not good practice for security
reasons.
• In the "Description" field, enter a brief description for the DSN. I entered "15Seconds Sample
DSN."
•From the "Server" drop-down box, select the SQL Server to which you would like to connect.
Since, my instance of SQL Server resides on the same machine where I am creating the DSN, I
selected "(local)."
• Click the "Next" button.
Figure 6
7. On the second "Create a New Data Source to SQL Server" (see Figure 7) dialog box:
• Select the "With SQL Server authentication using a login ID and password entered by the user"
radio button to indicate that database security with be implemented by SQL Server rather than
Windows NT.
• Select the "Connect to SQL Server to obtain default settings for the additional configuration
options" checkbox.
• In the Login ID textbox, enter "sa."
• Leave the "Password" textbox empty.
• Click the "Next" button.
Note: I used "sa" for login ID with a blank password for convenience. This is the default SQL
Server administrator account. Again, this is not a good idea for security reasons.
Figure 7
8. On the third "Create a New Data Source to SQL Server" dialog box (see Figure 8): • Select the
"Change the default database to" checkbox and select "15Seconds" from the accompanying select
box.
• Click the "Next" button.
Figure 8
9. On the fourth "Create a New Data Source to SQL Server" dialog box (see Figure 9): • Click the
"Finish" button.
Figure 9
10. On the "ODBC Microsoft SQL Server Setup" dialog box (see Figure 10), do one of the following:
•Click the "Test Data Source. . . " button to ensure that the ODBC connection has been created
successfully.
•Click the "OK" button to complete the ODBC DSN setup process.
Figure 10
We have a database, a table, some data and an ODBC DSN. The next step is to create an ASP page to
access the data.
1. Begin the ASP script with standard code (or at least my standard):
2. Dimension all variables we'll be using in the scripts. I like to dimension my variables in blocks for ease of
readability.
4. Set the connection objects ConnectionString property. Notice in the ConnectionString, we are specifying
"15Seconds" as the DSN, "sa" as the User ID (UID). This should look familiar because it is the same
information we provided while creating the ODBC DSN.
oConn.ConnectionString = "DSN=15Seconds;UID=sa"
OConn.Open
8. Now we will start an HTML table and create an HTML table row by iterating through the Recordset's
Field collection to create column headers for our HTML table.
9. Using the Record object's GetString method, we will create an HTML table row for each record in
t_articles:
oRS.Close
Set oRs = Nothing
oConn.Close
Set oConn = Nothing
Conclusion
This article details how to connect to the data source using ASP. Data access with ASP, at its core, is very
simple. However, there are many ways to improve and build upon the provided example. Try using OLE
DB rather than ODBC, or incorporating eXtensible Markup Language (XML) into your data access
processes.