·您的位置: 首页 » 资源教程 » 编程开发 » ASP » 一段返回随机记录的代码

一段返回随机记录的代码

类别: ASP教程  评论数:0 总得分:0
很久没用ACCESS了,只是看到经常碰到有人问如何返回随机记录的问题,所以就贴了上来,随便看看。

<!--#include virtual="/adovbs.inc"-->
<%
Dim objConn
Dim objRst
Dim strSQL
Dim strConnection
Dim str
Dim str1
Dim cnt
Dim cnt1
Dim rndMax
Dim RndNumber

strConnection="driver={Microsoft Access Driver (*.mdb)};dbq=" & server.mappath("/testdb.mdb")
strSQL = "SELECT id FROM tblQuestions"

set objConn = Server.CreateObject("ADODB.Connection")
Set objRst = Server.CreateObject("ADODB.Recordset")
objConn.Open strConnection
set objRst.ActiveConnection = objConn
objRst.LockType = adLockOptimistic
objRst.CursorType = adOpenKeySet
objRst.Open strSQL

objRst.MoveLast
cnt = objRst.RecordCount
cnt1 = cnt
rndMax = cnt

If CInt(Request.Form("maxNumber")) < cnt Then
cnt1 = CInt(Request.Form("maxNumber"))
End If

str = ","
str1 = ","

Do Until cnt1 = 0
Randomize
RndNumber = Int(Rnd * rndMax)

If (InStr(1, str1, "," & RndNumber & "," ) = 0) Then
str1 = str1 & RndNumber & ","
cnt1 = cnt1 - 1
objRst.MoveFirst
objRst.Move RndNumber
str = str & objRst("id") & ","
End If

Loop

objRst.Close
Set objRst = Nothing

sql = "SELECT * FROM tblQuestions WHERE (((InStr(1,\'" & str & "\',(\',\' & [id] & \',\')))<>0)) "
Set objRst = Server.CreateObject("ADODB.Recordset")
set objRst.ActiveConnection = objConn
objRst.LockType = adLockOptimistic
objRst.CursorType = adOpenKeySet
objRst.Open sql
%>
...DISPLAY THE RECORDS RETURNED...
<%
objRst.Close
Set objRst = Nothing

objConn.Close
Set objConn = Nothing
%>
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1