Web Workshop
Database Access using ASP
Ted Williams, MCSD, MCSE

List of Sections

Links to the various sections below:
8.0 Review
8.1 JavaScript & JScript
8.2 ASP Databases
8.3 References

Active Server Pages (ASP).

Active Server Pages(ASP). ASP is built into Microsoft's web server, named Internet Information Server (IIS). It is also available with the Personal Web Server on Windows 9x platforms. Third-party vendors, such as ChiliSoft have extended ASP to other web servers such as Netscape's web server and O'Reilly's WebSite for Windows. ASP is a platform for server-side programs written in either JScript or VBScript. JScript a Microsoft variant of JavaScript. VBScript is a subset of Visual Basic that is tailored to web programming.

The big advantage of ASP is that we can use a wide variety of COM components that provide additional functionality, such as email and database access.

Dynamic Pages. If the browser sends a URL requesting an HTML page to the web server, the web server just sends the contents of the file back to the browser without any modifications. If the browser sends a URL that specifies an executable program (*.exe), a Perl program (*.pl or *.cgi), or an ASP page (*.asp) the program may return a page that varies depending upon information received from the browser and upon information available at the server. Since the contents of the page may vary, pages returned from a program are called "dynamic pages".

Server-side Scripts. Scripting languages, such as JavaScript or VBScript can be executed on the web server. Scripts executed on the server are called "server-side scripts". ASP are distinguished by the .asp extension. ASP usually contain some scripts mixed in with ordinary HTML. Some web servers do not understand VBScript, but since we can choose what type of web server we use, the choice of languages is under our control.

Client-side ScriptsIn executing a server-side script, HTML is usually sent back to the browser. The page sent back to the browser may include JavaScript or VBScript. When this script is received by the browser, the script is executed by the browser. Scripts sent to the browser for execution are called "client-side" scripts. For example, Microsoft's browser, Internet Explorer (IE), can execute both VBScript and JavaScript. Unfortunately, not all browsers are capable of executing scripts, so client-side scripts must be used with caution. Web sites that cater to a wide variety of users do not use client-side scripts.

Since JavaScript works on a wider variety of server and browsers and we will stick to JavaScript for all of our ASP programming. Besides, most ASP examples are written in VBScript, our examples will supplement those found elsewhere.

8.0 Review

The Plan. Our plan is to implement each application three times, as listed below:

When writing the database program in C++ and in Perl, we interacted directly with the operating system to conduct a conversation with the database. In this ASP version of the program, we will just use the facility provided by ASP. Please review our earlier presentations at tlwilliams.net for the C++ and Perl versions.

This presentation is not a thorough discussion of ASP or JavaScript. Rather, we will focus more on some interesting examples of real ASP code.

Database Development. In developing a database for our use with C++, the Internet Database Connector (IDC) provided a simple way to interface a web page to a database using ODBC. In the version with Perl, the DBM (DataBase Management) library provided a way to implement a database. For many years, UNIX systems have a standard library called DBM that is also implemented for Perl in Win32 systems. DBM allows one to store an associative array (a hash) on disk. DBM provides persistent storage so that the associative array is saved between program executions. A hash is a Perl data structure that allows us to associate a database record with a key. This structure allows a simple way to have a table with a single index. More complex problems would require a more complicated approach.

8.1 JavaScript & JScript

JavaScript. The first incarnation of JavaScript, developed by Netscape, was named LiveScript. After Sun's Java language received a lot of notoriety, Netscape renamed LiveScript to JavaScript. JavaScript is a different language from Java developed by a different company. Since JavaScript is the only language that has been widely implemented by various browsers, it is currently the language of choice for all client-side scripts.

JScript. JScript is Microsoft's dialect of JavaScript. JScript contains several enhancements, including a mechanism for instantiating Active Server (COM) Components. Unfortunately, the database access methods used here are a non-standard feature of JScript.

8.2 ASP Databases

The ADO Interface. Active Data Objects (ADO) is a general object-oriented interface to a wide variety of databases. It uses ODBC (Open Database Connectivity) interface and other interfaces as an underlying mechanism. The big advantage of ADO is that it morphs a variety of interfaces into the ASP mold.

The ADO Object Model ADO has just three main objects, Connection, Recordset, and Command. The Connection object provides a connection to the data source. The Command object provides a way to fetch or modify records using SQL (Structured Query Language). The Recordset object provides a way to access individual records fetched by the SQL command.

Each of these objects has a collection of properties. The Recordset object has a Fields collection which provides information about the individual fields in the record.

The Connnection Object

Once an instance of a connection object is established, it may be used to open a connection with an existing database using the method Open. To open a database, a Data Source Name (DSN) must be created using the ODBC Administrator program from as described in our April discussion of ASP. For example, suppose that we wish to connect to the pubs database using the DSN Books.

Set oC = Server.CreateObject("ADODB.Connection")
oC.Open "DATABASE=pubs;DSN=Books;UID=sa;Password=;"
oC.Execute "DELETE * FROM Contact WHERE State = 'LA'"
oC.Close
Set oC = Nothing
Notes:

Fetching Records. SQL can also be used to fetch one or more records as shown in this example:

Set oC = Server.CreateObject("ADODB.Connection")
oC.Open "DATABASE=pubs;DSN=Books;UID=sa;Password=;"
sSQL = "SELECT * FROM Contact WHERE State = 'LA'"
Set oRs = oC.Execute(sSQL, lRecs, cmdText)
oRs.MoveFirst
Do While Not oRs.EOF
	For Each oField in oRs.Fields
		Response.Write oField.Value
	Next
	Ors.MoveNext
Loop
oRs.Close
Set oRs = Nothing
oC.Close
Set oC = Nothing
Notes:

8.3 References



Suggestions? Send email to: webmaster@tlwilliams.net
Last update: August 12, 2000