您现在的位置: 军旅同心-旅游自驾-军旅文学 >> 读书赏析 >> 学习园地 >> 电脑网络 >> 技术文章 >> 正文
一段返回随机记录的代码
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005-9-10 14:02:28
很久没用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
%>


更多
免责声明:作品版权归所属媒体与作者所有!!本站刊载此文不代表同意其说法或描述,仅为提供更多信息。如果您认为我们侵犯了您的版权,请告知!本站立即删除。有异议请联系我们。
文章录入:烟灰缸    责任编辑:烟灰缸 
网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
| 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 网站地图 | 版权申明 | 网站公告 | 管理登录 |