欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javascript asp教程Recordset记录

程序员文章站 2023-11-16 21:45:40
recordset is another created/instanciated object. it is a collection of data taken fro...

recordset is another created/instanciated object. it is a collection of data taken from a database. recordset has 26 properties, 25 methods, 11 events, and two (2) collections. the vast majority of recordset is beyond the scope of this web site.

quick aside:

the database we are using for lesson 16 and lesson 17 is an ms access 2000 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 id, colorname, and hexvalue.

the illustration below shows how we will cycle through these records.

id colorname hexvalue
1 aqua 00ffff
2 beige f5f5dc
3 crimson dc143c
4 darkviolet 9400d3
5 forestgreen 228b22
id colorname hexvalue
1 aqua 00ffff
2 beige f5f5dc
3 crimson dc143c
4 darkviolet 9400d3
5 forestgreen 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 will use the movenext() method to go from record #1 to #2, etc. then, we will 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 will focus on those lines of 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 that 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 that is undefined, asp will first look it up in the dll. you'll notice in the recorset.open() method that we use variables that are not defined in the script. thanks to the dll, we won't get an error code.

managing the recordset:

first 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 in 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 colorchart 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 do 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 is out there, on the internet, and in books. there is one other thing we have to talk about with recordsets. we do that in lesson 18.