Author: Please use the GetString () method to speed up your ASP script (to replace multiple rows of Response.Write).
Instance
Using GetString ()
How to use GetString () to display data in a recordset in an HTML table.
Multi-line Response.Write
The following example shows one way to display a database query in an HTML table:
The following are the referenced contents:
<body> <% Set Conn=server.createobject ("ADODB. Connection ") Conn. Provider= "Microsoft.Jet.OLEDB.4.0" Conn. Open "C:/webdata/northwind.mdb" Set rs = Server.CreateObject ("Adodb.recordset") Rs. Open "Select Companyname, ContactName from Customers", Conn %> <table border= "1" width= "100%" > <%do until Rs. Eof%> <tr> <td><%response.write (Rs.fields ("Companyname"))%></td> <td><%response.write (Rs.fields ("ContactName"))%></td> </tr> <%rs. MoveNext Loop%> </table> <% Rs.close Conn.close Set rs = Nothing Set conn = Nothing %> </body>
|
For a large query, this increases the processing time for the script, because the server needs to handle a large number of Response.Write commands.
The solution is to create all the strings, from <table> to </table>, and then output them-using only once Response.Write.
GetString () method
The GetString () method gives us the ability to display all strings using only one Response.Write at a time. And it doesn't even need do. Loop code and conditional tests to check whether the recordset is in EOF.
Grammar
str = Rs. GetString (format,rows,coldel,rowdel,nullexpr)
To create an HTML table using data from a recordset, we only need to use three of the above parameters (all of the parameters are optional):
Coldel-HTML used as a column separator
Rowdel-HTML used as a row delimiter
NULLEXPR-HTML used when column is empty
Note: the GetString () method is an attribute of ADO 2.0. You can download the ADO 2.0:http://www.microsoft.com/data/download.htm from the address below
In the following example, we will use the GetString () method to save the recordset as a string:
The following are the referenced contents:
<body> <% Set Conn=server.createobject ("ADODB. Connection ") Conn. Provider= "Microsoft.Jet.OLEDB.4.0" Conn. Open "C:/webdata/northwind.mdb" Set rs = Server.CreateObject ("Adodb.recordset") Rs. Open "Select Companyname, ContactName from Customers", Conn Str=rs. GetString (,, "</td><td>", "</td></tr><tr><td>", "") %> <table border= "1" width= "100%" > <tr> <td><%response.write (str)%></td> </tr> </table> <% Rs.close Conn.close Set rs = Nothing Set conn = Nothing %> </body>
|
The above variable str contains a string of all columns and rows returned by the SELECT statement. </td><td> appears between each column, </td></tr><tr><td> appears between each row. So, with only one Response.Write, we get the HTML we need.