Use GetRows of the RecordSet object in JScript in asp

Source: Internet
Author: User

When writing an ASP program, VBScript is usually used. However, you can also use JScript instead of this option. However, when JScript is used as the ASP language, it is slightly inconvenient than VBScript, for example, the GetRows method of RecordSet.
RecordSet objects are generally used to operate databases in ASP. If you focus on program efficiency, the GetRows method of the RecordSet object may be used to convert record set objects into arrays, the operation array is much faster than the MoveNext method of the RecordSet object, and the RecordSet object can be released as soon as possible after the array is retrieved, thus reducing resource usage, this is also a method to optimize ASP performance.
In VBScript, a two-dimensional array is obtained using the RecordSet. GetRows method. The data in the array can be obtained by traversing the array.
Assume that there is a database with a table named mytable and three fields named id, first, and second respectively. Copy codeThe code is 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 ")
'Retrieve record set
Set rs = conn. Execute ("SELECT id, first, second FROM mytable ")
'Get the data array
Data = rs. GetRows ()
'Close the record set and release the object.
Rs. Close ()
Set rs = Nothing
'Retrieve the number of records
RecN = UBound (data, 2)
'Circular output data
For I = 0 To recN
'Note: the array subscript starts from 0.
'Display database data
Response. Write ("ID:" & data (0, I) & ", First:" & data (1, I )&_
", Second:" & data (2, I) & "<br/> ")
Next
'Close the database connection and release the object
Conn. Close ()
Set conn = Nothing
%>

However, when using JScript, there is a problem, that is, JScript does not have a two-dimensional array. If you want to use the data obtained by GetRows, it is necessary to convert the two-dimensional array in VBScript into an array recognized by JScript, that is, the element is a one-dimensional array of the array.
In JScript, the array obtained using the GetRows method has a toArray method, which can be converted to an array that can be used in JScript, but this array is one-dimensional, that is, if you want to use it like VBScript, You need to convert it yourself.
After reading MSDN and searching for related articles on the internet, I wrote an array Conversion Function to use the GetRows method in JScript.Copy codeThe Code is as follows: <script language = "JScript" runat = "server">
// Code by xujiwei
// Http://www.xujiwei.cn/
// Define variables
Var conn, rs, vdata, data, recN, I;
// Connect to the database
Conn = Server. CreateObject ("ADODB. Connection ");
Conn. Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" +
Server. MapPath ("data. mdb "));
// Obtain the record set
Rs = conn. Execute ("SELECT id, first, second FROM test ");
// Obtain the data array and convert it to an available array type in JScript
Vdata = rs. GetRows (). toArray ();
// Obtain the number of fields in the data table
I = rs. Fields. Count;
// Close the record set and release the object
Rs. Close ();
Rs = null;
// Convert the Array
Data = transArray (vdata, I );
// Obtain the number of records
RecN = data. length;
// Output data cyclically
For (I = 0; I <recN; I ++ ){
// Note that the array subscript starts from 0
// Display data in the database
Response. Write ("ID:" + data [I] [0] + ", First:" + data [I] [1] +
", Second:" + data [I] [2] + "<br/> ");
}
// Close the database connection and release the object
Conn. Close ();
Conn = null;

// Array Conversion Function
// By xujiwei
// Parameter: array of objects obtained by the arr-GetRows method using the toArray Method
// Fieldslen-data table field count
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 data that is frequently updated but frequently used, you can cache the data array with the Application object after obtaining the data, thus reducing the number of queries to the database, optimizes ASP performance to a certain extent.
Go to 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.