SoFunction
Updated on 2025-04-13

asp ADO GetString function and using GetString to improve ASP speed page 2/2


Many Asp programmers have had the experience of executing database queries and then displaying the query results in the form of an html table. Usually this is what we do:

<% 
"create connection / recordset 
"populate data into recordset object 
%> 

<table> 
<% do while not  %> 
<tr> 
<td><%=rs("field1")%></td> 
<td><%=rs("field2")%></td> 
... 
</tr> 
<%  
loop %> 
</table> 

If there are many query results, the server will take a lot of time to explain your asp script because there are many statements to process. If you put all the output results in a very long string (from <table> to </table>), the server will just need to explain the statement once, and the speed will be much faster. Some capable guys at Microsoft have turned their ideas into reality. (Note, this is a feature that only ado 2.0 has. If you are still using ado 1.5, you can download ado 2.0 for free in /data/)

With the getstring method, we can use only one to display all outputs, which is like a do... loop loop that can determine whether the recordset is eof.

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

string = (stringformat, numrows, columndelimiter, rowdelimiter, nullexpr) 

GetString  getstring The method to generate an html table from the result of the recordset. We only need to care about 3 of the 5 parameters of getstring: columndelimiter (html code that separates the columns of the record set), rowdelimiter (html code that separates the rows of the record set), and nullexpr (html code that should be generated when the current record is empty). As you see in the example of generating the html table below, each column is separated by <td>...</td>, and each row is separated by <tr>...</tr>. Let's take a look at the code of the example.

<%@ language="vbscript" %> 
<% option explicit "good coding technique 

"establish connection to db 
dim conn 
set conn = ("") 
 "dsn=northwind;" 

"create a recordset 
dim rs 
set rs = ("") 
 "select * from table1", conn 

"store our one big string 
dim strtable 
strtable = (,,"</td><td>","</td></tr><tr><td>"," ") %> 

<html> 
<body> 


<table> 
<tr><td> 
<% (strtable) %> 
</tr></td> 
</table> 

</body> 
</html> 
<% 

"cleanup! 
 
set rs = nothing 
 
set conn = nothing 
%> 

The strtable string is used to store the code of the html table generated from the result "select * from table1". There will be </td><td> html code between each column of the html table, and the html code between each row is </td></td><tr><td>. The getstring method will output the correct html code and store it in the strtable, so that we can output all records in the dataset in just one row. 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, this string looks lengthy and messy, but it is the html code we want. (Note, in the handwritten html code, we put <table><tr><td> in front of the handwritten html code and </td></tr></table> behind it. This is because our formatted string does not contain the strings required for the beginning and end of these tables.)

The article by charles carroll:/learn/ tells how to use getstring to generate a select box. I think it will be very helpful to you.
Previous page12Read the full text