Getrows_ programming for using Recordset objects in JScript in ASP 10000 Q

Source: Internet
Author: User
When writing an ASP program, VBScript is always used, but it is not just a choice, it can also be used in JScript. However, when using JScript as the ASP's language, there are some small inconvenience than with VBScript, such as the GetRows method of the recordset.
Operating the database in ASP, generally need to use the Recordset object, if pay attention to the program efficiency, may use the Recordset object's GetRows method, converts the Recordset object to the array, The operation array will be much faster than the MoveNext method using the Recordset object, and the Recordset object can be released as early as possible after the array is taken out, thereby reducing resource occupancy, which is also a way to optimize ASP performance.
In VBScript, a two-dimensional array is fetched using the Recordset.getrows method, where the data can be obtained by traversing the array.
Suppose you now have a database with a table named MyTable and 3 fields with the name Id,first,second.
Copy Code code as follows:

' Code by Xujiwei
' http://www.xujiwei.cn/
' Define variables
Dim Conn,rs,data,recn,i
' Connect to the database
Set conn=server.createobject ("ADODB. Connection ")
Conn. Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &_
Server.MapPath ("Data.mdb")
' Get the recordset
Set Rs=conn. Execute ("SELECT Id,first,second from MyTable")
' Get an array of data
Data=rs. GetRows ()
' Close the recordset and release the object
Rs. Close ()
Set rs=nothing
' Get the number of records
Recn=ubound (data,2)
' Loop output data
For I=0 to RECN
' Note that the array subscript starts at 0
' Display data in a database
Response.Write ("ID:" &data (0,i) & ", A: &data (1,i) &_
", Second:" &data (2,i) & "<br/>")
Next
' Close database connection, release object
Conn. Close ()
Set conn=nothing
%>

However, when JScript is used, there is a problem that JScript does not have a two-dimensional array, and if you want to use the data obtained by GetRows, it is necessary to convert the two-dimensional array in this VBScript into an array that JScript recognizes, that is, a one-dimensional array of elements.
In JScript, an array obtained with the GetRows method has a ToArray method that can be converted into an array that can be used in JScript, but the array is one-dimensional, that is, if you want to use it as in VBScript, we need to do the conversion ourselves.
After reviewing MSDN and searching for relevant articles on the web, I wrote an array conversion function to use the GetRows method in JScript.
Copy Code code as follows:

<script language= "JScript" runat= "Server" >
Code by Xujiwei
http://www.xujiwei.cn/
Defining variables
var conn,rs,vdata,data,recn,i;
Connecting to a database
Conn=server.createobject ("ADODB.") Connection ");
Conn. Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +
Server.MapPath ("Data.mdb"));
Get recordset
Rs=conn. Execute ("SELECT id,first,second from Test");
Gets an array of data and converts to an array type available in JScript
Vdata=rs. GetRows (). ToArray ();
Get the number of fields in a data table
I=rs. Fields.Count;
Close the recordset, releasing the object
Rs. Close ();
Rs=null;
Converting arrays
Data=transarray (Vdata,i);
Get the number of records
Recn=data.length;
Looping output data
for (i=0;i<recn;i++) {
Note that the array subscript starts at 0
Displaying data in a database
Response.Write ("ID:" +data[i][0]+ "," the "+data[i][1]+
", Second:" +data[i][2]+ "<br/>");
}
Close database connections, releasing objects
Conn. Close ();
Conn=null;

Array Conversion Functions
by Xujiwei
Parameters: An array of objects obtained by the Arr-getrows method using the ToArray method
Fieldslen-Number of data table fields
function Transarray (Arr,fieldslen) {
var len=arr.length/fieldslen,data=[],sp;
for (Var i=0;i<len;i++) {
Data[i]=new Array ();
Sp=i*fieldslen;
for (Var j=0;j<fieldslen;j++)
DATA[I][J]=ARR[SP+J];
}
return data;
}
</script>

For some of the updated frequency is not high, and the use of more than the data, you can successfully obtain the data array, with the Application object to cache, so as to reduce the number of queries on the database, a certain degree of programming to optimize the performance of the ASP.
Transfer address: http://www.xujiwei.cn/blog/?id=717
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.