Warning: mkdir(): Permission denied in /data/pub/txcms/system/lib/Fso.php on line 151
天下站长网 ASP 一段返回随机记录的代码 - powered by andsky.com
投递文章 投稿指南 RSS订阅 站点通告:网站升级,部分数据正在导入之中,请等待..
您的位置:首页 >> Web编程 >> ASP

一段返回随机记录的代码

发布时间:2005-12-17 23:24:09  文章来源:  浏览次数:1589  
承德互联 专业的unix(freebsd)主机 无限子域 BGP多线 自主开发面版 更快 更稳 更安全
很久没用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
%>

作者:admin
与好友一起分享精彩内容:

收藏到QQ书签】【返回顶部】【返回首页
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 程序支持 - 网站地图 - 意见反馈 - 返回顶部