当前位置: 首页 > 图文教程 > 网络编程 > ASP > asp ADO GetString函数与用GetString来提高ASP的速度

ASP
asp生成三维饼图的函数
ASP下的简洁的多重查询的方法与函数 真不错
发一个ASP的ADODB类代码
A利用ASP小偷和Google实现在线翻译功能的代码
ASP类型网站结合动网论坛会员的方法
Asp下实现多表单域无组件文件上传的实例
asp制作中常用到的函数库集合
易心asp分页类 v1.0
asp中xmlhttp组件发包
检测是否为数字类型的asp代码
可以获得文件的文件名的asp函数
用asp实现访问远程计算机上MDB access数据库文件的方法
用ASP实现写IIS日志的代码
asp在IE浏览器中下载服务端上的各类文件的实现方法
asp汉字中文图片验证码的实现代码
asp WAP获取手机终端信息的一段代码
asp取得数组中的最大值的方法
用asp实现的截取指定格式字符串的代码
asp jmail发邮件 详细解析
ASP编程中连接数据库和数据库操作的常用代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 39 ::
收藏到网摘: n/a

没想到asp下有getstring这个东西,看样子不熟悉这块啊,以后考虑用这个,应该不错,另外还有getrows等,都是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()方法将记录集以一个字符串的形式保留:
复制代码 代码如下:
许多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. 我想对你们也是很有帮助的。