Runoob Tutorial ArchiveEnglish

ADO accelerates scripts using GetString()

ADO accelerates scripts using GetString()


Please use the GetString() method to accelerate your ASP script (instead of multiple lines of Response.Write).


Multiple lines of Response.Write

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

<%

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

%>

<%do until rs.EOF%>

  

    

    

  

<%rs.MoveNext

loop%>

<%Response.Write(rs.fields("Companyname"))%> <%Response.Write(rs.fields("Contactname"))%>

<%

rs.close

conn.close

set rs = Nothing

set conn = Nothing

%>

For a large query, this will increase the script processing time because the server needs to process a large number of Response.Write commands.

The solution is to create the entire string from

to
, and then output it - using Response.Write only once.


GetString() method

The GetString() method allows us to display all strings with only one Response.Write, and it doesn't even require do..loop code or conditional tests to check if the recordset is at EOF.

Syntax

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

To create an HTML table using data from the recordset, we only need to use three of the above parameters (all parameters are optional):

**Note:** The GetString() method is a feature of ADO 2.0. You can download ADO 2.0 from the following address:http://www.microsoft.com/data/download.htm

In the following example, we will use the GetString() method to store the recordset as a string:

Instance

<%

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(,,"",""," ")

%>

  

    

  

<%Response.Write(str)%>

<%

rs.close

conn.close

set rs = Nothing

set conn = Nothing

%>

The variable str above contains a string of all columns and rows returned by the SELECT statement. A comma appears between each column, and a newline appears between each row. Thus, with only one Response.Write, we get the required HTML.