音效素材网提供各类素材,打造精品素材网站!

站内导航 站长工具 投稿中心 手机访问

音效素材

asp中记录集对象的getrows和getstring用法分析
日期:2021-09-05 22:14:54   来源:脚本之家

GetRows 方法

将 Recordset 对象的多个记录复制到数组中。
语法

array = recordset.GetRows( Rows, Start, Fields )

返回值
返回二维数组。
参数
Rows 可选,长整型表达式,指定要检索记录数。默认值为 adGetRowsRest (-1)。
Start 可选,字符串或长整型,计算得到在 GetRows 操作开始处的记录的书签。也可使用下列 BookmarkEnum 值。

常量           说明
AdBookmarkCurrent   从当前记录开始。
AdBookmarkFirst     从首记录开始。
AdBookmarkLast     从尾记录开始。

Fields 可选,变体型,代表单个字段名、顺序位置、字段名数组或顺序位置号。ADO 仅返回这些字段中的数据。
说明
使用 GetRows 方法可将记录从 Recordset 复制到二维数组中。第一个下标标识字段,第二个则标识记录号。当 GetRows 方法返回数据时数组变量将自动调整到正确大小。
如果不指定 Rows 参数的值,GetRows 方法将自动检索 Recordset 对象中的所有记录。如果请求的记录比可用记录多,则 GetRows 仅返回可用记录数。
如果 Recordset 对象支持书签,则可以通过传送该记录的 Bookmark 属性值,来指定 GetRows 方法将从哪个记录开始检索数据。
如要限制 GetRows 调用返回的字段,则可以在 Fields 参数中传送单个字段名/编号或者字段名/编号数组。
在调用 GetRows 后,下一个未读取的记录成为当前记录,或者如果没有更多的记录,则 EOF 属性设置为 True。

GetString方法

查询数据库显示表格时,我们常用Do While()...Loop 或者是For...Next循环来显示表格,这样当我们要查询大量数据时,势必会比较慢。这时,我们就可以用记录集对象提供的GetString()方法(ADO必须升级到2.0)。
语法

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

参数说明:
objRecordset:已打开的记录集对象;
format:可选,一般取默认值(默认值为2)
n:可选,显示记录的数量,默认值为全部显示
coldel:可选,列界定符
rowdel:可选,行界定符
nullexpr:可选,该参数用于填充空字段!
有了GetString方法,我们就可以仅用一个Response.Write来显示所有的输出了,它就象是能判断Recordset是否为EOF的DO ... LOOP循环。
用这个方法,可以自动的循环输出字符串,就不用再去while或for循环了,只要建立了RS对象,并且执行了相应操作,不管那是返回一条或者多条记录,甚至是空记录,getstring照样工作。
要从Recordset的结果里生成HTML表格,我们只需关心GetString的5个参数中的3个:coldel(分隔记录集的列的HTML代码),rowdel(分隔记录集的行的HTML代码),和nullexpr(当前记录为空时应生成的HTML代码)。

<TABLE Border=1> 
<TR><TD> 
<% = Response.Write rs.GetString( , , "</TD><TD>", "</TD></TR><TR>", ) %> 
</TABLE> 

这样写的HTML结果如下:

<TABLE Border=1> 
<TR> 
<TD>row1, field1 value</TD> 
<TD>row1, field2 value</TD> 
</TR> 
<TR> 
<TD>row2, field1 value</TD> 
<TD>row2, field2 value</TD> 
</TR> 
</TABLE> 

这里有个BUG了,再看看生成下拉选单:

<% 
Set RS = conn.Execute("Select theValue,theText FROM selectOptionsTable orDER BY theText") 
optSuffix = "</OPTION>" & vbNewLine 
valPrefix = "<OPTION Value='" 
valSuffix = "'>" 
opts = RS.GetString( , , valSuffix, optSuffix & valPrefix, "--error--" ) 
' Next line is the key to it! 
opts = Left( opts, Len(opts)-Len(valPrefix) ) 

Response.Write "<Select ...>" & vbNewLine 
Response.Write valPrefix & opts 
Response.Write "</Select>" 
%> 

如果想建立一个正确的表格的话,解决那个BUG,只要这样做就可以了:

<% 
Set RS = conn.Execute("Select * FROM table") 
tdSuffix = "</TD>" & vbNewLine & "<TD> 
trPrefix = "<TR>" & vbNewLine & "<TD>" 
trSuffix = "</TD>" & vbNewLine & "</TR>" & vbNewLine & "<TR>" & vbNewLine 
opts = RS.GetString( , , tdSuffix, trSuffix & trPrefix, "--error--" ) 
' Next line is the key to it! 
opts = Left( opts, Len(opts)-Len(trPrefix) ) 
Response.Write "<TABLE Border=1 CellPadding=5>" & vbNewLine 
Response.Write trPrefix & opts 
Response.Write "</TABLE>" & vbNewLine 
%>

再介绍一个完全不同的办法:

<% 
SQL = "Select '<OPTION Value=''',value,'''>',text,'</OPTION>' FROM table orDER BY text" 
Set RS = conn.Execute(SQL) 
Response.Write "<Select>" & vbNewLine & RS.GetString(,,"",vbNewLine) & "</Select>" 
%> 

你用过吗。。。

看到了吗?可以直接从查询中返回结果。
再进一步,您可以这样做:

<% 
SQL = "Select '<OPTION Value=''' & value & '''>' & text & '</OPTION>' FROM table orDER BY text" 
Set RS = conn.Execute(SQL) 
Response.Write "<Select>" & vbNewLine & RS.GetString(,,"",vbNewLine) & "</Select>" 
%> 

下面是一份完整的示例:

Script Output:
711855 Wednesday 23 3/23/2005 1:33:37 AM
711856 Wednesday 23 3/23/2005 1:23:00 AM
711857 Wednesday 23 3/23/2005 1:26:34 AM
711858 Wednesday 23 3/23/2005 1:33:53 AM
711859 Wednesday 23 3/23/2005 1:30:36 AM

ASP完整代码如下:

<% 
' Selected constants from adovbs.inc: 
Const adClipString = 2 

' Declare our variables... always good practice! 
Dim cnnGetString ' ADO connection 
Dim rstGetString ' ADO recordset 
Dim strDBPath ' Path to our Access DB (*.mdb) file 
Dim strDBData ' String that we dump all the data into 
Dim strDBDataTable ' String that we dump all the data into 
' only this time we build a table 
' MapPath to our mdb file's physical path. 
strDBPath = Server.MapPath("db_scratch.mdb") 

' Create a Connection using OLE DB 
Set cnnGetString = Server.CreateObject("ADODB.Connection") 

' This line is for the Access sample database: 
'cnnGetString.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";" 
' We're actually using SQL Server so we use this line instead. 
' Comment this line out and uncomment the Access one above to 
' play with the script on your own server. 
cnnGetString.Open "Provider=SQLOLEDB;Data Source=10.2.1.214;" _ 
& "Initial Catalog=samples;User Id=samples;Password=password;" _ 
& "Connect Timeout=15;Network Library=dbmssocn;" 

' Execute a simple query using the connection object. 
' Store the resulting recordset in our variable. 
Set rstGetString = cnnGetString.Execute("Select * FROM scratch") 

' Now this is where it gets interesting... Normally we'd do 
' a loop of some sort until we ran into the last record in 
' in the recordset. This time we're going to get all the data 
' in one fell swoop and dump it into a string so we can 
' disconnect from the DB as quickly as possible. 
strDBData = rstGetString.GetString() 

' Since I'm doing this twice for illustration... I reposition 
' at the beginning of the RS before the second call. 
rstGetString.MoveFirst 

' This time I ask for everything back in HTML table format: 
strDBDataTable = rstGetString.GetString(adClipString, -1, _ 
&"</td><td>", "</td></tr>" & vbCrLf & "<tr><td>", " ") 

' Because of my insatiable desire for neat HTML, I actually 
' truncate the string next. You see, GetString only has 
' a parameter for what goes between rows and not a seperate 
' one for what to place after the last row. Because of the 
' way HTML tables are built, this leaves us with an extra 
' <tr><td> after the last record. GetString places the 
' whole delimiter at the end since it doesn't have anything 
' else to place there and in many situations this works fine. 
' With HTML it's a little bit weird. Most developers simply 
' close the row and move on, but I couldn't bring myself to' 
leave the extra row... especially since it would have a 
' different number of cells then all the others. 
' What can I say... these things tend to bother me. ;) 
strDBDataTable = Left(strDBDataTable, Len(strDBDataTable) - Len("<tr><td>")) 

' Some notes about .GetString: 
' The Method actually takes up to 5 optional arguments: 
' 1. StringFormat - The format in which to return the 
' recordset text. adClipString is the only 
' valid value. 
' 2. NumRows - The number of rows to return. Defaults 
' to -1 indicating all rows. 
' 3. ColumnDelimiter - The text to place in between the columns. 
' Defaults to a tab character 
' 4. RowDelimiter - The text to place in between the rows 
' Defaults to a carriage return 
' 5. NullExpr - Expression to use if a NULL value is 
' returned. Defaults to an empty string. 
' Close our recordset and connection and dispose of the objects. 
' Notice that I'm able to do this before we even worry about 
' displaying any of the data! 
rstGetString.Close 
Set rstGetString = Nothing 
cnnGetString.Close 
Set cnnGetString = Nothing 

' Display the table of the data. I really don't need to do 
' any formatting since the GetString call did most everything 
' for us in terms of building the table text. 
Response.Write "<table border=""1"">" & vbCrLf 
Response.Write "<tr><td>" 
Response.Write strDBDataTable 
Response.Write "</table>" & vbCrLf 
' FYI: Here's the output format you get if you cann GetString 
' without any parameters: 
Response.Write vbCrLf & "<p>Here's the unformatted version:</p>" & vbCrLf 
Response.Write "<pre>" & vbCrLf 
Response.Write strDBDataResponse.Write "</pre>" & vbCrLf 

' That's all folks! 
%> 

下面给大家补充一下

ASP里使用GetString来循环读取数据的具体方法及实例

在ASP里,我们要循环读取数据时,我们通常用的是 while ... wend   或者 do while() .. loop,但这种方法非常的没有效率,并且会影响网页程序打开的速度。

当我们只是从数据库里读取并显示数据的时候,我们可以用GetString来加速读取数据,并显示到网页上,使用GetString会大大增加我们从数据库读取数据的效率。

本文站长就和大家来说说ASP里使用GetString的方法,先来看GetString的具体语法结构:

Str=objRecordset.GetString(format,n,coldel,rowdel,nullexpr)
'参数:
objRecordset :为我们打开的rs记录集
format:可选,一般取默认值(默认值为2)
n:可选,显示记录的数量,默认值为全部显示
coldel:可选,列界定符
rowdel:可选,行界定符
nullexpr:可选,该参数用于填充空字段!

下面站长给大家提供一个完整的实例:

<%
set conn = server.CreateObject("adodb.connection")
conn.open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & server.MapPath("study.mdb")
set rs = server.createobject("adodb.recordset") 
sql = "sel ect * from users"
rs.open sql,conn,1,1 
str=rs.GetString(,,"</td><td>","</td></tr><tr><td>","该字段为空!")
Response.Write("<table border=1><tr><td>"&str&"</td></tr></table>")
%>

以上就是asp中记录集对象的getrows和getstring用法分析的详细内容,更多关于记录集 getrows getstring的资料请关注其它相关文章!

    您感兴趣的教程

    在docker中安装mysql详解

    本篇文章主要介绍了在docker中安装mysql详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编...

    详解 安装 docker mysql

    win10中文输入法仅在桌面显示怎么办?

    win10中文输入法仅在桌面显示怎么办?

    win10系统使用搜狗,QQ输入法只有在显示桌面的时候才出来,在使用其他程序输入框里面却只能输入字母数字,win10中...

    win10 中文输入法

    一分钟掌握linux系统目录结构

    这篇文章主要介绍了linux系统目录结构,通过结构图和多张表格了解linux系统目录结构,感兴趣的小伙伴们可以参考一...

    结构 目录 系统 linux

    PHP程序员玩转Linux系列 Linux和Windows安装

    这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,Linux和Windows安装nginx教程,具有一定的参考价值,感兴趣...

    玩转 程序员 安装 系列 PHP

    win10怎么安装杜比音效Doby V4.1 win10安装杜

    第四代杜比®家庭影院®技术包含了一整套协同工作的技术,让PC 发出清晰的环绕声同时第四代杜比家庭影院技术...

    win10杜比音效

    纯CSS实现iOS风格打开关闭选择框功能

    这篇文章主要介绍了纯CSS实现iOS风格打开关闭选择框,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作...

    css ios c

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的办法

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的

    Win7给电脑C盘扩容的办法大家知道吗?当系统分区C盘空间不足时,就需要给它扩容了,如果不管,C盘没有足够的空间...

    Win7 C盘 扩容

    百度推广竞品词的投放策略

    SEM是基于关键词搜索的营销活动。作为推广人员,我们所做的工作,就是打理成千上万的关键词,关注它们的质量度...

    百度推广 竞品词

    Visual Studio Code(vscode) git的使用教程

    这篇文章主要介绍了详解Visual Studio Code(vscode) git的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...

    教程 Studio Visual Code git

    七牛云储存创始人分享七牛的创立故事与

    这篇文章主要介绍了七牛云储存创始人分享七牛的创立故事与对Go语言的应用,七牛选用Go语言这门新兴的编程语言进行...

    七牛 Go语言

    Win10预览版Mobile 10547即将发布 9月19日上午

    微软副总裁Gabriel Aul的Twitter透露了 Win10 Mobile预览版10536即将发布,他表示该版本已进入内部慢速版阶段,发布时间目...

    Win10 预览版

    HTML标签meta总结,HTML5 head meta 属性整理

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析HTML代码,更好地将移动web前端页面表现出来...

    移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家...

    移动端 html5 长按

    HTML常用meta大全(推荐)

    这篇文章主要介绍了HTML常用meta大全(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    cdr怎么把图片转换成位图? cdr图片转换为位图的教程

    cdr怎么把图片转换成位图? cdr图片转换为

    cdr怎么把图片转换成位图?cdr中插入的图片想要转换成位图,该怎么转换呢?下面我们就来看看cdr图片转换为位图的...

    cdr 图片 位图

    win10系统怎么录屏?win10系统自带录屏详细教程

    win10系统怎么录屏?win10系统自带录屏详细

    当我们是使用win10系统的时候,想要录制电脑上的画面,这时候有人会想到下个第三方软件,其实可以用电脑上的自带...

    win10 系统自带录屏 详细教程

    + 更多教程 +
    ASP编程JSP编程PHP编程.NET编程python编程