javascript asp教程Recordset记录
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.
|
|
|
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
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.
推荐阅读
-
javascript asp教程第五课--合二为一
-
javascript asp教程第二课--转义字符
-
javascript asp教程第九课--cookies
-
javascript asp教程第四课 同时使用vbscript和javascript
-
javascript asp教程第三课 new String() 构造器
-
javascript asp教程第六课-- response方法
-
javascript asp教程第七课--response属性
-
javascript asp教程服务器对象
-
javascript asp教程第十课--global asa
-
javascript asp教程第十三课--include文件