JavaScript asp Tutorials more about Recordsets_asp basics

Source: Internet
Author: User

Below we'll attempt to access data from a database without knowing the column names. Clearly the best way to utilize data in your The database are to keep track of your schema. Schema is the layout of data in your database. The concept is beyond the scope of this web site, but it is worth mentioning. Most good resources on SQL would also be good the 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&lT;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 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 are 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. 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" 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") 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 some Thing 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 ("&l T;/table> ") RS.
Close ();
RS = null;
Connectobj.close ();
Connectobj = null;
 %> </BODY> </HTML>

Click here to run the script in a new window.

Notice when we do getRows () we don t get the column names (but that would is really easy to fix). The problem with myarray are that it's not very useful in its raw state. So we use a modulo operator and a little thing called RS. Fields.Count We can tell how many times we write the table before staring a new table row.

If you like the new VBArray constructor your should know that you have the following methods: dimensions () Item () LBound () ToArray () and UBound().

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.