The Recordset is another created/instanciated Object. It is a collection of the data taken from a database. Recordset has properties, methods, events, and two (2) collections. The vast majority of the Recordset is beyond the scope of this web site.
Quick aside:
The database we are using for lesson and lesson are an MS Access-file (not the best database for large sites). The file is called Htmlcolor.mdb and it has a single Table called Colorchart. The database Table has three columns and they are called IDs, ColorName, and Hexvalue.
The illustration below shows how we'll cycle through these records.
aqua
| ID |
Col ORName |
hexvalue |
| 1 | TD bgcolor= "#000000" >
00ffff |
| 2 |
beige |
f5f5dc |
| 3 |
Crimson |
dc143c |
| 4 |
darkviolet |
9400d3 |
| 5 |
forestgreen< /td> |
228b22 |
|
| ID |
C Olorname |
hexvalue |
| 1 |
Aqua |
00FFFF |
| 2 |
bei GE |
F5F5DC |
| 3 |
Crimson |
dc143c |
| 4 |
darkviolet |
9400d3 |
| 5 |
forestgreen< /td> |
228b22 |
|
| ID |
ColorName |
Hexvalue |
| 1 |
Aqua |
00FFFF |
| 2 |
Beige |
F5f5dc |
| 3 |
Crimson |
dc143c |
| 4 |
Darkviolet |
9400d3 |
| 5 |
Forestgreen |
228b22 |
|
In this example we cycle through the records row by row. We'll use the " MoveNext" () method of the "Go" #1 to #2, etc. Then, we'll access specific data in each row by using the column name.
Get started:
Below is the script for Lesson 17. It ' s exactly the same as the script for lesson 16. The difference is that we'll focus on those lines's code that deal with recordsets.
<% @LANGUAGE = "JavaScript"%> <!--METADATA type= "typelib" file= "C:\Program Files\Common" files\system\ado\ Msado15.dll "--> <HTML> <BODY> <% var myconnect =" Provider=Microsoft.Jet.OLEDB.4.0;
Data source= ";
Myconnect + = Server.MapPath ("\");
Myconnect + = "\\GlobalScripts\\htmlColor.mdb;"; var connectobj = Server.CreateObject ("ADODB.")
Connection "); var RS = Server.CreateObject ("ADODB.")
Recordset ");
var sql= "Select ID, ColorName, hexvalue from Colorchart;";
Connectobj.open (Myconnect); Rs.
Open (Sql,connectobj,adopenforwardonly,adlockreadonly,adcmdtext);
Response.Write ("<table border=\" 1\ "cellspacing=\" 0\ ">\r");
Response.Write ("<TR><TH>ID</TH><TH>colorName</TH>");
Response.Write ("<th>hexvalue</th></tr>\r"); while (! Rs.
EOF) {Response.Write ("<TR><TD>" +rs ("ID") + "</td><td bgcolor=\" # ");
Response.Write (RS ("Hexvalue") + "\" > "+ rs (" colorname ")); Response.Write ("</TD><TD>" +rS ("Hexvalue") + "</td></tr>\r"); Rs.
MoveNext ();
} Response.Write ("</table>\r"); Rs.
Close ();
Connectobj.close ();
RS = null;
Connectobj = null;
%> </BODY> </HTML>
Click here to run the script in a new window.
The DLL:
We ' ll dissect the Recordset statements from top to bottom. In this case, the means starting with the Dynamic Link Library.
<!--METADATA type= "typelib"
file= "C:\Program Files\Common Files\system\ado\msado15.dll"-->
What in the ' World is a ' DLL? A Dynamic Link Library holds values without tying up memory. Here's how it works. If you try to use a variable this is undefined, ASP'll the It up in the DLL. You'll notice in the Recorset.open () method of that "we use variables" are not defined in the script. To the DLL, we are won ' t get an error code.
managing the Recordset:
We must instanciate a Recordset, which we do in the little snippet below.
var RS = Server.CreateObject ("ADODB.") Recordset ");
Next we create a Text Command. The text in question comes the form of S.Q.L. (Structured Query Language). SQL is beyond the "scope of this web site", but the command below means select ID, ColorName and Hexvalue from the Colorchar T Table.
var sql= "Select ID, ColorName, hexvalue from Colorchart;";
Then we open the Recordset. The five arguments in order Are:source, connection, cursor type, lock type, and options.
Rs. Open (Sql,connectobj,adopenforwardonly,adlockreadonly,adcmdtext);
Using a loop, we tell ASP to does certain things for as long as we haven ' t hit EOF, which means end of File.
while (! Rs. EOF)
Next, we access specific collums in the Recordset by name.
Response.Write (RS ("Hexvalue") + "\" > "+ rs (" colorname "));
Then we move down from one row of records to the next.
Rs. MoveNext ();
Finally, when we reach EOF, the loop relinquishes control and we can close the Recordset Object.
Rs. Close ();
The Rest of the Story:
This is by no means a complete lesson on recordsets; It wasn ' t meant to be. The rest of the story on recordsets are out there, on the Internet, and in books. There is one of the other thing we are have to talk about with recordsets. We do so in Lesson 18.