ASP随机读取数据库中指定条数的记录
[ 2008-01-18 21:17:37 | Author: colacat ]
这几天在琢磨在线考试系统,发现其它方面实现起来没多少困难,而如何随机读取数据库中指定条数的记录?这个之前一直以为不是问题的问题倒难住了我。
通过网上查找和蓝色理想上网友的帮助,有两种方法可以实现:
第一种:
Comments Feed: http://www.colacat.cn/feed.asp?q=comment&id=2
通过网上查找和蓝色理想上网友的帮助,有两种方法可以实现:
第一种:
<%'以下代码为随机读取数据库中100条的记录
'连接数据库
sql="select * from test"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if not rs.bof and not rs.eof then
call disrndrecord(100,rs.recordcount)
sub disrndrecord(disnum,rsbound)
if rsbound<disnum then disnum=rsbound
for i=0 to disnum-1
thisrnd=getrnd(rsbound)
rs.move(thisrnd)
response.write rs("title")&"<br>"
rs.move(-thisrnd)
next
end sub
function getrnd(bound)
randomize()
rannum=int(bound*rnd)
if instr(appeared,"["&rannum&"]") then
rannum=getrnd(bound)
end if
appeared=appeared&"["&rannum&"]"
getrnd=rannum
end function
end if
rs.close
set rs=nothing
'关闭数据库%>
第二种方法:'连接数据库
sql="select * from test"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if not rs.bof and not rs.eof then
call disrndrecord(100,rs.recordcount)
sub disrndrecord(disnum,rsbound)
if rsbound<disnum then disnum=rsbound
for i=0 to disnum-1
thisrnd=getrnd(rsbound)
rs.move(thisrnd)
response.write rs("title")&"<br>"
rs.move(-thisrnd)
next
end sub
function getrnd(bound)
randomize()
rannum=int(bound*rnd)
if instr(appeared,"["&rannum&"]") then
rannum=getrnd(bound)
end if
appeared=appeared&"["&rannum&"]"
getrnd=rannum
end function
end if
rs.close
set rs=nothing
'关闭数据库%>
<%'以下代码为随机读取100条记录
'连接数据库
randomize()
intrandomnumber=int(1000*rnd)+1
sql="select top 100 * from test order by rnd("&(-1*intrandomnumber)&"*id)"
set rs=conn.execute(sql)
do while not rs.eof
response.write rs("title")&"<br>"
rs.movenext
loop
rs.close
set rs=nothing
conn.close
set conn=nothing
'关闭数据库%>
'连接数据库
randomize()
intrandomnumber=int(1000*rnd)+1
sql="select top 100 * from test order by rnd("&(-1*intrandomnumber)&"*id)"
set rs=conn.execute(sql)
do while not rs.eof
response.write rs("title")&"<br>"
rs.movenext
loop
rs.close
set rs=nothing
conn.close
set conn=nothing
'关闭数据库%>
Comments Feed: http://www.colacat.cn/feed.asp?q=comment&id=2
There is no comment on this article.






