javascript asp教程More About Recordsets

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().

(0)

相关推荐

  • javascript asp教程More About Recordsets

    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 th

  • 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. Quic

  • javascript asp教程创建数据库连接

    While this section is devoted to ASP database utilization, it very important to remember that this web site is not intended to be a thorough ASP resource. Remember, the focus of this site is strictly limited to how to use JavaScript as your primary s

  • javascript asp教程第十二课---session对象

    Overview: The Session Object is how you track a single user across many pages. It has four (4) properties, two (2) collections, one (1) method, and two (2) events. Get Started: In this series of examples we will create a password system. We will use

  • javascript asp教程第十课--global asa

    Global.asa: First of all, what is a global.asa? It's an optional script file that holds certain "global" information that you can access through the entire ASP appliciation. The global.asa is a plain text file saved with the .asa extension. You

  • javascript asp教程第六课-- response方法

    response 对象:reponse是asp中六个对象之一.它代表了服务器端对浏览器的回应.response有8种方法,9种属性和一个集.在这一课,我们就重点讲述方法.方法:在javascript中,asp方法使用括号.请注意依赖response.buffer的两个方法,我们将在下一课讲到他们.同样应该注意到addheader()和redirect(),因为他们必须优先于write()执行.所有的方法都在上面描述和演示了.下面我将详细讲述每一个方法.我将花点额外的时间来讲述我们用的最多的两个方

  • javascript asp教程第七课--response属性

    Below is a table of Response Properties along with examples and explanations. Response Properties Buffer Response.Buffer = trueAllows for the buffering of output CacheControl Response.CacheControl="Public" Sets Cache to "Public" or &qu

  • javascript asp教程第三课 new String() 构造器

    开始:new String() 是本课程计划中较早出现的另一个让人感觉到奇怪的地方.但和转义字符一样, new String()是创建一个成功的asp javascript应用的必须元素.下面是本课的两个脚本:下面是实际上承担重量的脚本:行为中的new String( ):现在我们来看看下面的asp行.Request.Form 我们将在后面有独立的课程来讲授.下面才是我们现在要讲的重点.在request.form中所持有的数据(来自用户的数据)并不是一个javascript数据类型.相反,它是一

  • javascript asp教程第二课--转义字符

    并非是题外话:我们在第二课中要讲到的"转义字符"看起来与我们的课程并没有关系.事实并非如此.转义字符是在 asp中使用javascript的一个完整部分.而第二课要讲的恰恰就是转义字符.没有转义字符,那么你为了 写出有效率的asp脚本将会遇到许多的困难.抱歉,下面进入正题:以下是第二课的asp脚本.还记不记得在第一课的列子中,我们怎样编写了一个html输出都在同一行的代码.转义字符允许我们增加 换行符号.下面是我们上边例子在客户端html输出页面的代码:换行符:我又在下面重新打了一行a

  • javascript asp教程错误处理

    The ASPError Object has zero (0) Methods, nine (9) Properties, zero (0) Events, and zero (0) Collections. AspCode AspDescription Category Column Description File Line Number SourceThe way you access the ASPError Properties is with a Server Method. Ye

随机推荐