您现在的位置是:网站首页> 编程资料编程资料
javascript asp教程More About Recordsets_ASP基础_
2023-05-25
322人已围观
简介 javascript asp教程More About Recordsets_ASP基础_
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"%><% 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("| " + RS.Fields(x).Name + " | "); x++; } getFieldNames = true; x = 0; Response.Write("
|---|
| " + RS.Fields(x).Value + " | "); x++; } Response.Write("
Click Here 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 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"%><% 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).
\r"); Response.Write(myArray + "
\r") RS.MoveFirst(); var myVBArray = new VBArray(RS.GetRows()) Response.Write("We can use the new VBArray constructor and the ") Response.Write("getItem( ) method. For example: myVBArray.getItem(1,1) ") Response.Write("returns " + myVBArray.getItem(1,1) + "
\r") Response.Write("Now lets make something useful.
\r") Response.Write("| " + myArray[x] + " | ") if ((x+1)%RS.Fields.Count==0) { Response.Write("
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().
相关内容
- javascript asp教程Recordset记录_ASP基础_
- javascript asp教程创建数据库连接_ASP基础_
- javascript asp教程错误处理_ASP基础_
- javascript asp教程服务器对象_ASP基础_
- javascript asp教程第十三课--include文件_ASP基础_
- javascript asp教程第十二课---session对象_ASP基础_
- javascript asp教程第十一课--Application 对象_ASP基础_
- javascript asp教程第十课--global asa_ASP基础_
- javascript asp教程第九课--cookies_ASP基础_
- ASP中类Class相关内容的整理资料_ASP CLASS类_
