JS in the use of methods RecordSet object GetRows

Author:Anonymous    Updated:2008-2-26 21:58:04
ASP written procedures, in most cases always use VBScript, but this is not just an option, and can also use JScript. However, in using the language of JScript as ASP, VBScript than some minor inconvenience, such as the GetRows RecordSet methods.
Operate in ASP database, it usually will use RecordSet object, if the focus on the efficiency of procedures, and may be used RecordSet object GetRows methods, the record set targets converted into an array, and an array of operating speed will be used in RecordSet object MoveNext method much faster, and the array can be removed as soon as possible after the release of RecordSet object, thereby reducing the resources of the occupier, this is also optimize the performance of the ASP method.

In VBScript, RecordSet.GetRows Methods used to is a two-dimensional array, the data can be passed inside to iterate method to obtain.

Assume that there is now a database, a table called mytable, three fields, respectively id name, first, second.

Code:
'Code by xujiwei
'Http://www.xujiwei.cn/
'Definition of variables
Dim conn, rs, data, recN, i
'Connecting to the database
Set conn = Server.CreateObject ( "ADODB.Connection")
Conn.Open "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" & _
     Server.MapPath ( "data.mdb")
'Get Set Records
Set rs = conn.Execute ( "SELECT id, the first, second FROM mytable")
'Access to an array of data
Rs.GetRows data = ()
'Off the record set targets released
Rs.Close ()
Set rs = Nothing
'Get Record
RecN = UBound (data, 2)
'Cycle of the output data
For i = 0 To recN
     'Note, array subscript start from 0
     'Show in the database data
     Response.Write ( "ID:" & data (0, i) & "First:" & data (1, i) & _
         "Second:" & data (2, i) & "<br />")
Next
'Close database connection, the release of Object
Conn.Close ()
Set conn = Nothing
%>
However, in JScript use, there will be a problem, and that is not two-dimensional array and JScript, if GetRows use the data collected from this VBScript will be necessary in the two-dimensional array can be converted into JScript array of identification, an array element is a one-dimensional array.

In JScript, with an array of GetRows acquire a toArray, can be converted into use in JScript array, but this is one-dimensional array, that is, if we use the same as in VBScript so, it is also necessary do our own conversion.

MSDN access to the Internet and search the related article, I wrote an array conversion function for use in JScript GetRows methods.

Code:
<script Language="JScript" runat="server">
/ / Code by xujiwei
/ / Http://www.xujiwei.cn/
/ / Definition of variables
Var conn, rs, vdata, data, recN, i;
/ / Database connections
Conn = Server.CreateObject ( "ADODB.Connection");
Conn.Open ( "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" +
     Server.MapPath ( "data.mdb"));
/ / Get the record set
Rs = conn.Execute ( "SELECT id, the first, second FROM test");
/ / Get the data array, and JScript conversion become available in the array type
Vdata = rs.GetRows (). ToArray ();
/ / Get the data sheet for the field
I = rs.Fields.Count;
/ / Set off the record, the release of Object
Rs.Close ();
Rs = null;
/ / Array conversion
Data = transArray (vdata, i);
/ / Get a few records
RecN = data.length;
/ / Cycle output data
For (i = 0; i <recN; i + +) (
     / / Note that the array subscript start from 0
     / / Display data in the database
     Response.Write ( "ID:" + data [i] [0] + "First:" + data [i] [1] +
         "Second:" + data [i] [2] + "<br />");
)
/ / Close database connection, the release of Object
Conn.Close ();
Conn = null;
/ / Array conversion function
/ / By xujiwei
/ / Parameters: arr - GetRows method with the object of the array method toArray
/ / Fieldslen - Data Table few fields
Function transArray (arr, fieldslen) (
     Var len = arr.length / fieldslen, data = [], sp;
     For (var i = 0; i <len; i + +) (
         Data [i] = new Array ();
         Fieldslen * sp = i;
         For (var j = 0; j <fieldslen; j + +)
             Data [i] [j] = arr [sp + j];
     )
     Return data;
)
</ Script>
For some update frequency is not high, and the frequency of use more data can be successful in obtaining an array of data, using Application object to cache, thereby reducing the number of accesses to the database, certain procedures extent, the performance of optimized ASP
Previous:ASP in the inside pages of the register DLL VBScript CLASS
Next:NO
User Reviews
Site Search
Related Articles
Recommended article
AD