Using GetString to increase the speed of ASP

Source: Internet
Author: User
Tags html form

Many ASP programmers have the experience of executing a database query and then displaying the results of the query in an HTML form. That's what we usually do:

<%
'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 the query results are many, the server explains your ASP script will take a lot of time, because there are many

The Response.Write statement is to be processed. If you put all the results of the output in a long string (from <table > to </table >), the server simply interprets the Response.Write statement a lot faster. Some of the most capable guys in Microsoft have turned their ideas into reality. (Note that this is a feature of ADO 2.0. If you are still using the previous version, please upgrade to the latest version)

With the GetString method, we can display all the output with just one Response.Write, which is like a do ... that can tell if the recordset is EOF. Loop loop.

The use 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's results, we only care about 3 of the 5 parameters of GetString:

ColumnDelimiter (HTML code that separates the columns of the Recordset), RowDelimiter (the HTML code that separates the rows of the Recordset), and nullexpr (the HTML code that should be generated when the current record is empty). As you can see in the example below that generates an HTML table, each column is <TD >...</td > delimited, with <tr >...</tr > delimited for each row. Let's take a look at the example 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 ><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
%>

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.