Using GetString to improve ASP speed

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

The strtable string is used to store the code for the HTML table we generated from the "SELECT * from table1" result.

There will be </td ><td > HTML code between each column of the HTML table, and the HTML code between each line is </td ></td ><tr ><td. The GetString method will output the correct HTML code and store it in strtable so that we can output all the records in the dataset in just one line Response.Write. Let's take a look at a simple example, assuming that our query results return the following rows and columns:

Col1 Col2 Col3
Row1 Bob Smith 40
Row1 Ed Frank 43
Row1 Sue Void 42

Then the string returned by the GetString statement will be:

Bob</td ><td >Smith</td ><td >40</td ><td ></td ></tr ><tr ><
td
>Ed ...

To be honest, the string looks long and messy, but it's the HTML code we want. (Note that we put <table ><tr ><td > in front of Response.Write in the hand-written HTML code, </TD ></tr > put it in the back. This is because our formatted string does not contain the strings required for the ends of these tables.

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.