<%
'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 to), 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: This is a feature that is available only in ADO 2.0 and later versions. 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 ):
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 (HTML that separates the columns of the record set Code ), 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] is separated, and each line is separated by [tr]... [/tr. Sample Code:
The following is a reference clip:
<% @ 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 HTML code of [/td] [td] between each column, the HTML code for each line is [/td] [/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:
The following is a reference clip:
Col1 col2 col3
Row1 bob smith 40
Row1 Ed Frank 43
Row1 Sue void 42
The string returned by the getstring statement is:
The following is a reference clip:
Bob </TD> <TD> Smith </TD> <TD> 40 </TD> </tr> <
TD
> Ed... bob </TD> <TD> Smith </TD> <TD> 40 </TD> </tr> <TD> ed. ..
This string looks lengthy and messy, but it is the desired HTML code. (Note that we will put it behind the manually written HTML code. This is because our formatted string does not contain the strings required for the beginning and end of the table .)