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

javascript asp教程More About Recordsets

程序员文章站 2023-11-16 21:45:16
below we will attempt to access data from a database without knowing the column names....

below we will attempt to access data from a database without knowing the column names. clearly the best way to utilize data in your database is to keep track of your schema. schema is the layout of data in your database. the concept is well beyond the scope of this web site, but it is worth mentioning. most good resources on sql will also be good resources on database management. better database schema leads to better asp code.

get started:

below is the script for lesson 18.

<%@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 * from colorchart;";

connectobj.open (myconnect);
rs.open(sql,connectobj,adopenforwardonly,adlockreadonly,adcmdtext);

var recordcount = rs.fields.count;
var x = 0;
var getfieldnames = false;

response.write("<table border=\"1\" cellspacing=\"0\">\r");
while (!rs.eof)
	{
	if (x >= recordcount)
		{
		x = 0
		}
	response.write("<tr>");
	if (!getfieldnames)
		{
		while (x <= recordcount-1)
			{
			response.write("<th>" + rs.fields(x).name + "</th>");
			x++;
			}
		getfieldnames = true;
		x = 0;
		response.write("</tr>\r<tr>")
		}
	while (x <= recordcount-1)
		{
		response.write("<td>" + rs.fields(x).value + "</td>");
		x++;
		}
	response.write("</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.

i don't think this needs much explaining. the rs.fields.count tells us how many columns wide the recordset is. for each row, we loop through columns using either rs.fields(x).name for the colum name or rs.fields(x).value for the datum in said column.

another way:

a potentially more elegant way to accomplish this same goal is to use the ado method getrows. it returns a multi-dimensional array containing the recordset data. wait! aren't javascript arrays lexical (and flat)? yes. we can emulate multi-dimensional arrays, but in reality they are flat. so it's a no-go on the getrows... unless we do something really creative.

<%@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 * from colorchart;";
connectobj.open (myconnect);
rs.open(sql,connectobj,adopenforwardonly,adlockreadonly,adcmdtext);

var myarray = rs.getrows().toarray();
response.write("let's see the results of myarray as javascript");
response.write(" sees them (which is flat).<br>\r");
response.write(myarray + "<br><br>\r")

rs.movefirst();
var myvbarray = new vbarray(rs.getrows())
response.write("we can use the <i>new vbarray</i> constructor and the ")
response.write("<i>getitem( )</i> method. for example: myvbarray.getitem(1,1) ")
response.write("returns " + myvbarray.getitem(1,1) + "<br><br>\r")

response.write("now lets make something useful.<br>\r")
response.write("<table border=1 cellpadding=0 cellspacing=0>")
response.write("\r<tr>")
for (var x=0; x<=myarray.length-1; x++)
	{
	response.write("<td>" + myarray[x] + "</td>")
	if ((x+1)%rs.fields.count==0)
		{
		response.write("</tr>\r<tr>")
		}
	}
response.write("</tr>\r")
response.write("</table>")
rs.close();
rs = null;
connectobj.close();
connectobj = null;
%>
</body>
</html>

click here to run the script in a new window.

notice when we use getrows( ) we don't get the column names (but that would be really easy to fix). the problem with myarray is that it's not very useful in its raw state. so we use a modulo operator and thanks to a little thing called rs.fields.count we can tell how many times we write data to the table before staring a new table row.

if you like the new vbarray constructor you should know that you have the following methods: dimensions() getitem() lbound() toarray() and ubound().