Javascript asp tutorial More About Recordsets

Source: Internet
Author: User

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. RS. Fields. Count Tells us how does 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 MethodGetRows. It returns a multi-dimen=array containing the Recordset data. WAIT! Aren't JavaScript Arrays lexical (and flat )? Yes. We can emulate multi-dimen1_arrays, but in reality they are flat. So it's a no-go onGetRows... 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 useGetRows ()We don't get the column names (but that wocould 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 calledRS. Fields. CountWe can tell how many times we write data to the table before staring a new table row.

If you likeNew VBArrayConstructor you shoshould know that you have the following methods:Dimensions () getItem () lbound () toArray ()AndUbound ().

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.