Be prepared: This lesson is going to be quite a whirlwind tour. We'll be
touching briefly, and in little detail, on all the topics you need to
make slick, data-driven pages. So sit back and get ready for the onslaught
of info.
Assuming you sat through Lesson
2, you should now have a data source name that is
defined as Parking and a table within Parking called Cars. If I
want to print out a list of all the cars in this table in alphabetical
order, all I need is the following code.
<%
SQL="SELECT carName FROM Cars ORDER BY carName"
set conn = server.createobject("ADODB.Connection")
conn.open "parking"
set cars=conn.execute(SQL)
%>
<% do while not cars.eof %>
<%= cars(0) %> <br>
<%cars.movenext
loop%>
<% cars.close %>
You probably recognize <br> as a standard HTML tag, but the other
stuff may look totally new. There are, however, some things you can pick up
from careful examination of this code. For instance, the line that starts
<% do while looks like it begins the kind of loop you'd find in any
programming language.
But I'm not here to make you guess. I only get paid if you learn. So
let's decipher the code together.
The first thing you should know is that there's more than one
thing going on here. In addition to the small amount of HTML you noticed
already, you'll find SQL (structured query language), VBScript (Visual Basic Scripting Edition), and Microsoft's ADO (ActiveX Data Objects) rearing their lovely heads. In the pages ahead, I'll deal with each of them. And since you've been such a great audience, I'm going to throw in a quick primer on the use of HTML forms in combination with all the other stuff.
Before I begin, let's take a look at the first couple
of lines of this script. If you've read Kevin's introduction to ASP, you know ASP (active server pages) is simply a collection of five objects. One of these, the Server
object, is called in the code set conn
=server.createobject("ADODB.Connection"). This line creates a new
Connection object with the variable name conn, which opens a
connection to the server. The next line, conn.open "parking", uses
the open method of the Connection object to establish a tie to the parking
database.
In the following line, set cars=conn.execute(SQL), the
conn object is being used to execute an SQL statement (which is
stored in a variable created in the first line) against the database, which
in turn loads an ADO recordset into the variable Cars.
Confused? Read on - this will make perfect sense soon.
next page»