Speed
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 will spend a lot of time explaining your ASP script because there are a lot of Response.Write statements 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 once more quickly. 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're still using ADO 1.5, you can download ADO 2.0 at http://www.microsoft.com/data/download.htm free)
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 result of the recordset, we only care about 3 of the 5 parameters of GetString: ColumnDelimiter (the HTML code that separates the recordset's columns), RowDelimiter (the HTML code that separates the rows of the Recordset), and nullexpr (HTML code that should be generated when the current record is empty). As you can see in the example below that generates HTML tables, each column is delimited by < TD >...</td >, each line is delimited by < TR >...</tr >. 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 >
<%
' 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. Each column in the HTML table will have </TD >< td > HTML code, 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 in our hand-written HTML code, we put < TABLE >< TR >< TD > in front of the Response.Write to the </td ></tr ></table &G t; put it in the back. This is because our formatted string does not contain the strings required for the ends of these tables.
Charles Carroll's article: Http://www.learnasp.com/learn/dbgetstring.asp describes how to use GetString to generate a select box. I think it will be very helpful to you, too.