asp ADO GetString函数与用GetString来提高ASP的速度

  rs.getstring的方法GetString的作用是:以字符串的形式返回指定的记录集。可以使用这个方法向ASP文件中添加HTML表格。

  getstring 方法语法

  Set str=objRecordset.GetString(format,n,coldel,rowdel,nullexpr)

  

Parameter参数 Description描述
format Optional. A StringFormatEnum value that specifies the format when retrieving a Recordset as a string

  可选参数。指定一个

n

  Optional. The number of rows to be converted in the Recordset

  RecordsetStringFormatEnum值。它是用于指定提取记录集的格式

coldel Optional. If format is set to adClipString it is a column delimiter. Otherwise it is the tab character

  可选参数。如果format[格式]值设置为adClipString,那么它就是一个列界定符;除此之外,它便是一个tab[制表符]

rowdel Optional. If format is set to adClipString it is a row delimiter. Otherwise it is the carriage return character

  可选参数。可选参数。如果format[格式]值设置为adClipString,那么它就是一个行界定符;除此之外,它便是一个carriage return [回车符]

nullexpr Optional. If format is set to adClipString it is an expression used instead of a null value. Otherwise it is an empty string

  可选参数。可选参数。如果format[格式]值设置为adClipString,那么它就是一个用于替代空值的表达式;除此之外,它便是一个空字符

案例

  To create an HTML table with data from a recordset, we only need to use three of the parameters above:

  我们只要通过上述三个参数中的一个就可以创建HTML格式的记录集数据表:

  coldel - the HTML to use as a column-separator

  coldel – 使用HTML格式作为列分隔符

  rowdel - the HTML to use as a row-separator

  rowdel – 使用HTML格式行分隔符

  NullExpr - the HTML to use if a column is NULL

  NullExpr – 如果列为空,则使用HTML

  Note: The GetString() method is an ADO 2.0 feature.

  在下面的案例中,我们将使用GetString()方法将记录集以一个字符串的形式保留:

  

复制代码 代码如下:

  <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", connstr=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 = Nothingset conn = Nothing%></body>

  </html>

  

Constant

  常量

Value

  值

Description

  描述

adClipString 2 Delimits rows by the rowdel parameter, columns by the coldel parameter, and null values by the nullexpr parameter

  指定rowdel参数对行(记录)进行界定;通过coldel参数对列(字段)进行界定,通过nullexpr参数对空值进行界定

  许多asp程序员都有过执行数据库查询,然后将查询结果用html表格的形式显示出来的经历吧. 通常我们是这么做的:

  <%

  "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>

  如果查询结果很多,服务器解释你的asp script将花费大量的时间,因为有许多的response.write语句要处理. 如果你将输出的全部结果放在一个很长的字符串里(从<table>到</table>),那么服务器只需解释一遍response.write语句,速度就会快得多. 微软公司里的一些能干的家伙已经将想法变成了现实. (注意,这是一个ado 2.0才有的特性. 如果你还在使用ado 1.5话,可以在http://www.microsoft.com/data/download.htm免费下载ado 2.0)

  有了getstring方法,我们就可以仅用一个response.write来显示所有的输出了,它就象是能判断recordset是否为eof的do ... loop循环.

  getstring的用法如下(所有的参数都是可选的):

  string = recordset.getstring(stringformat, numrows, columndelimiter, rowdelimiter, nullexpr)

  GetString rs.getstring getstring 方法要从recordset的结果里生成html表格,我们只需关心getstring的5个参数中的3个: columndelimiter(分隔记录集的列的html代码),rowdelimiter(分隔记录集的行的html代码),和nullexpr(当前记录为空时应生成的html代码). 就象你在下面生成html表格的例子里所看到的那样,每列用<td>...</td>分隔,每行用<tr>...</tr>分隔. 来看看例子的代码吧.

  <%@ 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>

  </html>

  <%

  "cleanup!

  rs.close

  set rs = nothing

  conn.close

  set conn = nothing

  %>

  strtable字符串用于存放我们从"select * from table1"结果生成的html表格的代码. html表格的每列之间都将有</td><td>的html代码,每行之间的html代码是</td></td><tr><td>. getstring方法将输出正确的html代码并存放在strtable中,这样我们只需一行response.write便可以输出数据集中的所有记录. 让我们来看个简单的例子,假设我们的查询结果返回了以下的行和列:

  col1 col2 col3

  row1 bob smith 40

  row1 ed frank 43

  row1 sue void 42

  那么getstring语句返回的字符串将是:

  bob</td><td>smith</td><td>40</td><td></td></tr><tr><td>ed ...

  说实话,这个字符串看上去冗长而杂乱,但它就是我们想要的html代码. (注意看,我们在手工书写的html代码中,将<table><tr><td>放在response.write的前面,将</td></tr></table>放在它的后面. 这是因为我们的格式化字符串中并不含有这些表格头尾所需的字符串.)

  charles carroll的文章:http://www.learnasp.com/learn/dbgetstring.asp讲述了如何用getstring来生成一个select box. 我想对你们也是很有帮助的。