欢迎光临
我们一直在努力

随机访问recordset的一条记录_asp技巧

建站超值云服务器,限时71元/月

假设这个数据表有一个唯一的ID字段,并至少有一条记录。随机存取其中一条记录的方法是非常简单的,可以分为四步:
1、取得记录总数n。
2、把所有的ID号存储到一个数组中
3、产生一个不大于n的随机数m
4、从数组中取出第m个ID号,查询数据表,取得记录数据。
  下面是部分代码:
$#@60;%
set conn = Server.CreateObject(‘ADODB.Connection‘)
conn.open ‘$#@60;conn string$#@62;‘

‘ ***** (step 1) *****

set rs = conn.execute(‘Select count(id) from someTable‘)
rCount = rs(0)

‘ ***** (step 2) *****

set rs = conn.execute(“select id from someTable”)
cnt = 1
dim RRs
redim RRs(rCount)
do while not rs.eof
RRs(cnt) = rs(0)
cnt = cnt + 1
rs.movenext
loop

‘ ***** (step 3) *****

randomize
currentRR = cLng(rnd*rCount+0.5)
ID = RRs(currentRR)

‘ ***** (step 4) *****

sql = “select otherfield from someTable where id=” & ID
set rs = conn.execute(sql)
response.write “ID # ” & ID & “ = ” & rs(0)
rs.close: set rs = nothing
conn.close: set conn = nothing
%$#@62;
  对于SQL Server,还有更加有效率的方法。比如设计两个存储过程。我这里只是阐明一些思路,并希望这种思路可以同时用在Access和SQL Server中。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 随机访问recordset的一条记录_asp技巧
分享到: 更多 (0)

相关推荐

  • 暂无文章