Latest web development tutorials

ADO via GetString () script acceleration

Please use the GetString () method to speed up your ASP scripts (instead of multiple lines of Response.Write).


Multi-line Response.Write

The following example demonstrates one way to display a database query in an HTML table:

<html>
<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>
</html>

For a large query, doing so would increase the processing time of the script, because the server needs to handle a large number of Response.Write command.

The solution is to create a whole string from <table> to </ table>, then output - used only once Response.Write.


GetString () method

GetString () method gives us the ability to use only one Response.Write, you can display all the strings. At the same time it does not even need do..loop the code and the conditional test to check whether the record set in EOF.

grammar

str = rs.GetString(format,rows,coldel,rowdel,nullexpr)

To create an HTML table using the data from the record set, we only need to use more than three parameters (all parameters are optional):

  • coldel - used as a column delimiter HTML
  • rowdel - used as a line delimiter HTML
  • nullexpr - When the column is empty when using HTML

Note: GetString () method is ADO 2.0 features. You can download from the address below 2.0 the ADO: http://www.microsoft.com/data/download.htm

In the following example, we will use the GetString () method, the record set saved as a string:

Examples

<html>
<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>","&nbsp;")
%>

<table border="1" width="100%">
<tr>
<td><%Response.Write(str)%></td>
</tr>
</table>

<%
rs.close
conn.close
set rs = Nothing
set conn = Nothing
%>
</body>
</html>

The above variable str contains a string of all the columns and rows returned by a SELECT statement. In between each column will appear </ td> <td>, in between each row will appear </ td> </ tr> <tr> <td>. Thus, with only one Response.Write, we get the required HTML.