程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 一段返回隨機記錄的代碼

一段返回隨機記錄的代碼

編輯:ASP技巧

很久沒用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
%>


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved