When writing ASP programs, VBScript is generally used, but this is not the only option. JScript can also be used. But when using JScript as the language of ASP, there are some minor inconveniences compared to using VBScript, such as the GetRows method of RecordSet.
When operating a database in ASP, the RecordSet object is generally used. If you focus on program efficiency, you may use the GetRows method of the RecordSet object to convert the record set object into an array. The operation of the array will be faster than using the RecordSet object. The MoveNext method is much faster, and can release the RecordSet object as soon as possible after taking out the array, thereby reducing resource usage. This is also a way to optimize ASP performance.
In VBScript, what is obtained by using the RecordSet.GetRows method is a two-dimensional array, and the data inside can be obtained by traversing the array.
Suppose there is a database with a table named mytable and three fields named id, first, and second.
Copy the code code as follows:
'codebyxujiwei
'http://www.xujiwei.cn/
'Define variables
Dimconn,rs,data,recN,i
'Connect to database
Setconn=Server.CreateObject(ADODB.Connection)
conn.OpenProvider=Microsoft.Jet.OLEDB.4.0;DataSource=&_
Server.MapPath(data.mdb)
'Get the recordset
Setrs=conn.Execute(SELECTid,first,secondFROMmytable)
'Get the data array
data=rs.GetRows()
'Close the recordset and release the object
rs.Close()
Setrs=Nothing
'Get the number of records
recN=UBound(data,2)
'Loop output data
Fori=0TorecN
'Note that array subscripts start from 0
'Display data in database
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()
Setconn=Nothing
%>
But when using JScript, there will be a problem, that is, JScript does not have a two-dimensional array. If you want to use the data obtained by GetRows, you must convert the two-dimensional array in VBScript into an array that JScript can recognize, that is, the elements are A one-dimensional array of arrays.
In JScript, the array obtained using the GetRows method has a toArray method, which can be converted into an array that can be used in JScript. However, this array is one-dimensional, which means that if we want to use it like in VBScript, we still need to do it ourselves. to do the conversion.
After consulting MSDN and searching related articles on the Internet, I wrote an array conversion function for using the GetRows method in JScript.
Copy the code code as follows:
<scriptlanguage=JScriptrunat=server>
//codebyxujiwei
//http://www.xujiwei.cn/
//Define variables
varconn,rs,vdata,data,recN,i;
//Connect to database
conn=Server.CreateObject(ADODB.Connection);
conn.Open(Provider=Microsoft.Jet.OLEDB.4.0;DataSource=+
Server.MapPath(data.mdb));
//Get the record set
rs=conn.Execute(SELECTid,first,secondFROMtest);
//Get the data array and convert it into an array type available in JScript
vdata=rs.GetRows().toArray();
//Get the number of fields in the data table
i=rs.Fields.Count;
//Close the recordset and release the object
rs.Close();
rs=null;
//convert array
data=transArray(vdata,i);
//Get the number of records
recN=data.length;
//Loop output data
for(i=0;i<recN;i++){
//Note that array subscripts start from 0
//Display the 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
//byxujiwei
//Parameters: arr-The object obtained by the GetRows method and the array obtained by the toArray method
//fieldslen-the number of fields in the data table
functiontransArray(arr,fieldslen){
varlen=arr.length/fieldslen,data=[],sp;
for(vari=0;i<len;i++){
data[i]=newArray();
sp=i*fieldslen;
for(varj=0;j<fieldslen;j++)
data[i][j]=arr[sp+j];
}
returndata;
}
</script>
For some data that is not updated frequently but has been used frequently, you can use the Application object to cache it after successfully obtaining the data array, thereby reducing the number of queries to the database and optimizing the performance of ASP to a certain extent.