Use getstring to multiply ASP speed

Source: Internet
Author: User
Use getstring to multiply ASP speed. Source: Internet browsing times:

239


Many ASP programmers have performed database queries and then displayed the query results in the form of HTML tables. Normally we should do this:

 

<%
'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 determining whether recordset is an EOF do... loop.

The usage of getstring is as follows (all parameters are optional ):

 

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.

 

<% @ 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:

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.