Use getrows or getstring to speed up ASP

Source: Internet
Author: User
Tags dsn


<%
''' the basic usage of getrows '''''''' '''
call dbconnbegin ()
set rs = server. createobject ("ADODB. recordset ")
SQL =" select top 10 ID, areacode, areaname, englishname from info_hotel "
Rs. open SQL, Conn, 1, 1
reclist = Rs. getrows ()
Rs. close

%>
<Table width = "600" border = "0" cellspacing = "0" cellpadding = "0">
<% For I = 0 to ubound (reclist, 2) %>
<Tr>
<TD> <% = reclist (0, I) %> </TD>
<TD> <% = reclist (2, I) %> </TD>
<TD> <% = reclist (3, I) %> </TD>
</Tr>
<% Next %>
</Table>

<%
''' Getrows is used to implement the paging of database records. Code --------------------
Dim istart, ioffset
Istart = request ("START ")
Ioffset = request ("offset ")
If not isnumeric (istart) or Len (istart) = 0 then
Istart = 0
Else
Istart = CINT (istart)
End if
If not isnumeric (ioffset) or Len (ioffset) = 0 then
Ioffset = 30
Else
Ioffset = CINT (ioffset)
End if
Response. Write "Viewing" & ioffset & "records starting at record" & istart & "<br>"
Dim objconn, objrs
Set objconn = server. Createobject ("ADODB. Connection ")
'Objconn. Open "DSN = MP3"
Dim connstr
String 9
Dim DB
DB = "csnjimageman. mdb"
Connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("" & dB &"")
Objconn. Open connstr
Set objrs = server. Createobject ("ADODB. recordset ")
Objrs. Open "select * From imageinfo", objconn
Dim aresults
Aresults = objrs. getrows
Objrs. Close
Set objrs = nothing
Objconn. Close
Set objconn = nothing

Dim irows, icols, irowloop, icolloop, Istop
Irows = ubound (aresults, 2)
Icols = ubound (aresults, 1)
If irows> (ioffset + istart) then
Istop = ioffset + istart-1
Else
Istop = irows
End if
For irowloop = istart to Istop
For icolloop = 0 to icols
Response. Write aresults (icolloop, irowloop )&""
String 5

Next
Response. Write "<br>"
Next
Response. Write "<p>"
If istart> 0 then
'Show Prev Link
Response. Write "<a href =" "getrows. asp? Start = "& istart-ioffset &_
"& Offset =" & ioffset & ""> previous "& ioffset &" </a>"
End if
If Istop <irows then
'Show next link
Response. Write "<a href =" "getrows. asp? Start = "& istart + ioffset &_
"& Offset =" & ioffset & ""> next "& ioffset &" </a>"
End if
%>

-------------------------------------------------------

Ado also provides a more efficient way to obtain data. The getrows method returns a two-dimensional array variable. Each row corresponds to a record in the recordset and each column corresponds to the field in the record. The syntax of this method is as follows:

Vararray = Rs. getrows ([rows], [start], [fields])

Rows indicates the number of records to be read. If you want to obtain all records of the recordset, use-1 or omit this parameter. Start indicates the first bookmarked to be read. It can also be one of the following enumerated constants: 0-adbookmarkcurrent (current record), 1-adbookmarkfirst (first record), or 2-adbookmarklast (last record ).

Fields is an array of optional field names used to limit the amount of data to be read. (You can also specify a single field name, a single field index, or a field Index Array ). When the rows value is set to less than the number of records in the recordset, the First unread record becomes the current record. If the rows parameter is omitted or set to-1-adgetrowsrest or greater than the number of unread records, the getrows method reads all records and puts the recordset In the EOF state without generating any errors.

When processing the data of the destination variable array, remember that the data storage method is a bit opposite: the first dimension of the array defines the recordset field (Data row ), the second dimension defines the recordset data column. The following is an example of loading a certain field of all records in recordset:

Dim values as variant, fldindex as integer, recindex as integer
Values = Rs. getrows (, array ("lastname", "firstname", "birthdate "))
For recindex = 0 to ubound (values, 2)
For fldindex = 0 to ubound (values)
Print values (fldindex, recindex ),
Next
Print
Next

The getrows method is usually faster than reading a record at a time, but when using this method, you must ensure that the recordset does not contain too many records; otherwise, it is easy to fill up all the memory with a very large array of variables. For the same reason, be careful not to include any blob (Binary Large Object) or clob (character large object) fields; if so, applyProgramIt will definitely crash, especially for large recordset. Finally, remember that the array of variables returned by this method is based on 0; the number of records returned is ubound (values, 2) + 1, and the number of returned fields is ubound (value, 1) + 1.

The getstring method is similar to the getrows method, but it returns multiple records in the form of a single string. The getstring syntax is as follows:

Getstring ([format], [numrows], [coldelimiter], [rowdelimiter], [nullexpr])

Format is the format of the result. Getstring may also support more formats, but currently the only supported format is 2-adclipstring, so there is no choice. Numrows is the number of columns to be obtained. (Use-1 or omit this parameter to read all the remaining records .) Coldelimiter is a line delimiter (by default, it is a Tab character ). Rowdelimiter is the delimiter of a record (by default, it is a line break ). Nullexpr is a string used to represent the null field (default is a Null String ). The file indicates that only the last three parameters can be used when format = adclipstring, but this warning does not make much sense, because (as previously mentioned) This format is currently only supported. The following example uses the getstring method to export data to a text file separated by semicolons:

dim I as long
open "datafile.txt" for output as #1
for I = 0 to Rs. fields. count _ 1 'export field names.
If I> 0 then print #1, ";";
Print #1, Rs. fields (I ). name;
next
Print #1, ""
Rs. movefirst 'export data.
Print #1, Rs. getstring (, ";", vbcrlf); 'don' t add an extra CR-LF here.
close #1

The getstring method does not allow you to export only the subsets of fields, nor modify the order of export fields. If you need these additional functions, use the getrows method and create a result string.
 
 
 
 
 
 
 
 
 
 
-------------Use getstring to speed up ASP-------------------------
 
 
 
Many ASP programmers have performed database queries and displayed the query results in HTML tables. We usually do this:

Reference content is as follows:  <%
'Create connection/recordset
'Populate data into recordset object
%>
<Table>
<% Do while not Rs. EOF %>
<Tr>
<TD> <% = RS ("field1") %> </TD>
<TD> <% = RS ("field2") %> </TD>
.
</Tr>
<% Rs. movenext
Loop %>
</Table>
If there are many query results, the server will spend a lot of time explaining your asp script, because there are many
Response. write statement to process. if you place all the output results in a long string (from <Table> to </table>), the server only needs to explain the response once. write statement, the speed will be much faster. some competent Microsoft companies have turned their ideas into reality. (note that this feature is available only when ADO 2.0 or above is used. if you are still using a previous version, upgrade to the latest version)
With the getstring method, we can use only one response. Write to display all the output. It is like a do... loop that can judge whether recordset is EOF.
The usage of getstring is as follows (all parameters are optional ):

Reference content is as follows:  String = recordset. getstring (stringformat,
Numrows,
Columndelimiter,
Rowdelimiter, nullexpr)
To generate an HTML table from the recordset result, we only need to care about three of the five parameters of getstring:
Columndelimiter (the HTML code that separates the columns of the record set), rowdelimiter (the HTML code that separates the rows of the record set), and nullexpr (the HTML code generated when the current record is empty ). as you can see in the example of generating an HTML table below, each column uses <TD>... </TD> separated by <tr>... </tr>. let's take a look at the sample code.

Reference content is as follows:  <% @ Language = "VBScript" %>
<% Option explicit 'good coding technique
'Establish connection to DB
Dim Conn
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "DSN = northwind ;"
'Create A recordset
Dim rs
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open "select * From Table1", Conn
'Store our one big string
Dim strtable
Strtable = Rs. getstring (, "</TD> <TD>", "</TD> </tr> <TD>"
,"")
%>
<HTML>
<Body>
<Table>
<Tr> <TD>
<% Response. Write (strtable) %>
</Tr> </TD>
</Table>
</Body>
</Html>
<%
'Cleanup!
Rs. Close
Set rs = nothing
Conn. Close
Set conn = nothing
%>
The strtable string is used to store the code of the HTML table generated from the "select * From Table1" result.
The HTML table contains the </TD> <TD> HTML code between each column, the HTML code for each line is </TD> <tr> <TD>. the getstring method saves the correct HTML code in strtable, so we only need a line of response. write to output all records in the dataset. let's take a look at a simple example. Assume that the following rows and columns are returned for our query results:

Reference content is as follows: Col1 col2 col3
Row1 bob smith 40
Row1 Ed Frank 43
Row1 Sue void 42
Then the string returned by the getstring statement is:

Reference content is as follows: Bob </TD> <TD> Smith </TD> <TD> 40 </TD> </tr> <
TD
> Ed...

To be honest, this string looks lengthy and messy, but it is the HTML code we want. (Note: In the manually written HTML code, place <Table> <tr> <TD> in response. before write, put </TD> </tr> </table> behind it. this is because our formatted strings do not contain the strings required for the header and tail of these tables .)

 

from: http://nyazheng.blog.163.com/blog/static/5712646520088554940528/

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.