Many ASP programmers have had the experience of executing database queries and then displaying the query results in HTML tables. Usually we do this:
| Here is a quote: <% '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 a lot of query results, it will take a lot of time for the server to interpret your ASP script because there are many Response.Write statements to process. If you put all the output results in a long string (from to), then The server only needs to interpret the Response.Write statement once and it will be much faster. Some capable guys at Microsoft have turned the idea into reality. (Note that this is a feature only available in ADO 2.0 and above. If you are still using the previous version, please upgrade to the latest version)
With the GetString method, we can use only one Response.Write to display all output. It 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 = recordset.GetString(StringFormat, NumRows, ColumnDelimiter, RowDelimiter, NullExpr)
To generate an HTML table from the results of the Recordset, we only need to care about 3 of the 5 parameters of GetString:
ColumnDelimiter (the 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 of generating an HTML table below, each column is separated by... and each row is separated by.... Example code:
| Here is a quote: <%@ 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 Dimrs 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 Setrs=Nothing conn.Close Set conn = Nothing %> |
The strTable string is used to store the code of the HTML table we generated from the results of SELECT * FROM table1.
There will be HTML code between each column of the HTML table, and the HTML code between each row is. The GetString method will output the correct HTML code and store it in strTable, so that we only need one line of Response.Write to output the data set All records of . Let's look at a simple example, assuming that our query results return the following rows and columns:
| Here is a quote: Col1 Col2 Col3 Row1 Bob Smith 40 Row1 Ed Frank 43 Row1 Sue Void 42 |
The string returned by the GetString statement will be:
| Here is a quote: Bob</td ><td >Smith</td ><td >40</td ><td ></td ></tr ><tr >< td >Ed... |
This string looks long and messy, but it is the desired HTML code. (Note that we will put it after it in the hand-written HTML code. This is because our formatting string does not contain the strings required for the header and footer of these tables.)