javascript asp教程More About Recordsets
Below we will attempt to aess 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>
to run the script in a new window.
I don't think this needs much explaining. The
Another Way:
A potentially more elegant way to acplish 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>
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().
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程