ASP+Access随机读取数据库记录的方法

两种方法,都利用了随机数!

方法一: 

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

<!--#include file="qiconn.asp"-->

<%

Randomize(timer())

bgsql = "select top 5 fuid,id,lcolor,name from lei order by rnd(-(id + " & Int((10000 * Rnd) + 1) & "))"

set bgrs=server.CreateObject("adodb.recordset")

bgrs.open bgsql,conn,1,1

 if bgrs.eof and bgrs.eof then

  response.write ""

 else

  do until bgrs.eof

  

   response.write bgrs("name") & "<br>"

  bgrs.movenext

  loop

 end if

bgrs.closd

set bgrs=nothing

%>

</body>

</html>

方法二、 

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

<!--#include file="qiconn.asp"-->

<%

dim rndnum

 Randomize  

 Do While Len(rndnum)<2

 num1=CStr(Chr((57-48)*rnd+48))

 rndnum=rndnum&num1

 loop

 Randomize 

bgsql = "select * from lei where id>"& rndnum &" order by rnd(id) desc"

set bgrs=server.CreateObject("adodb.recordset")

bgrs.open bgsql,conn,1,1

 if bgrs.eof and bgrs.eof then

  response.write ""

 else

  do until bgrs.eof

  

   response.write bgrs("name") & "<br>"

  bgrs.movenext

  loop

 end if

bgrs.closd

set bgrs=nothing

%>

</body>

</html>