Low cost ecommerce web development India flash website design
The first few lines are the opening HTML tags for any page. There's no ASP
code within them. The ASP block begins with the statement,
Dim DB
which is a declaration of the variable that we're gonna use later on. The
second line,
Set DB = Server.CreateObject (“ADODB.Connection”)
does the following two things:
Firstly, the right-hand-side statement, Server.CreateObject() is used to
create an instance of a COM object which has the ProgID
ADODB.Connection. The Set Statement then assigns this reference to our
variable, DB. Now, we use the object just created to connect to the database
using a Connection String.
The string,
"PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" +
“C:\Databases\Students.mdb”
is a string expression that tells our object where to locate the database, and
more importantly, what type the database is – whether it is an Access
database, or a Sybase database, or else, is it Oracle. (Please note that this is a
Connection String specific to Access 2000 databases. This example does not
use ODBC.)
36
If the DB.Open statement succeeds without an error, we have a valid
connection to our database under consideration. Only after this can we begin
to use the database.
The immediate next lines,
Dim RS
Set RS = Server.CreateObject (“ADODB.Recordset”)
serve the same purpose as the lines for creating the ADODB.Connection
object. Only now we're creating an ADODB.Recordset!
Now,
RS.Open “SELECT * FROM Students”, DB
is perhaps the most important line of this example. Given an SQL statement,
this line executes the query, and assigns the records returned to our Recordset
object. The bare-minimum syntax, as you can see, is pretty straight-forward.
Of course, the Recordset.Open (...) method takes a couple of more
arguments, but they are optional, and would just complicate things at this
juncture.
Now, assuming that all the records we want are in our Recordset object, we
proceed to display it.
If RS.EOF And RS.BOF Then
Response.Write “There are 0 records.”
In any scenario where it is expected that no records might exist, this is an
important error check to be performed. In case your query returned no results,
the Recordset.BOF (beginning of file) & Recordset.EOF (end of file) are
both True at the same time. So you can easily write an If-statement to
perform a very basic error check. (If you don't do this now, you'll encounter
errors in the later part of the script. It's always wise to prevent rather than
cure errors.)
We shall look at the next few lines as a complete block and not as separate
lines of code.
Else
RS.MoveFirst
While Not RS.EOF
Response.Write RS.Fields (“FirstName”)
Response.Write RS.Fields (“LastName”)
Response.Write “<HR>”
37
RS.MoveNext
Wend
End If
RS.MoveFirst is a method that moves the record pointer (for now, consider
this to be an imaginary structure that always points to the current record in the
Recordset) to the First record. By default, it may or may not be positioned
correctly, so it is imperative to position it before you begin any operations.
Then we have a While-loop that iterates through all the records contained in
the Recordset. The condition that we check is that RS.EOF should be False.
The moment it is True, it can be inferred that there are no more records to be
found.
RS.Fields(“FirstName”) retrieves the value of the “FirstName” field of
the current record. We use a Response.Write statement to write it out to the
page. Similarly, we write the RS.Fields (“LastName”) after the first name.
You may also use a shortcut syntax for this, which takes the form:
RS (”FirstName”)
After you're done displaying, you must advance the record pointer to the next
record, so you execute a RS.MoveNext. And that's all you wanted to do within
the loop, so you end the loop now. Just write Wend and the loop ends! And so
does our little example!
Moving on to complex queries
Queries are what get complex, not the method of accessing data! So no matter
what data you want, you use exactly the same syntax in your code. The only
thing that changes is the SQL statement, and perhaps, the fields that you
actually display on the page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73