asp中记录集对象的getrows和getstring用法分析
程序员文章站
2022-07-02 08:43:31
getrows 方法 将 recordset 对象的多个记录复制到数组中。 语法 复制代码 代码如下: array = recordset.getrows( rows, s...
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!
%>
将 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!
%>
下一篇: crontab定时备份mysql数据