How can asp+access randomly draw records in a database?
Ask for a different smoke each time.
There's a saying on the web. Select top * from Tab_st ORDER by Rnd (ID)
But in this way, each pumping is the same, which makes no sense.
Please advise the more appropriate method.
Simple and efficient: Do not repeat random read database records
Set rs = Server.CreateObject ("Adodb.recordset")
Rs.Open sql,conn,1,1
DIM appeared
Call Disrndrecord (10,rs.recordcount) ' Invoke function to display records at this location
' ############### #SUBS ################
' #DisRndRecord (Disnum,rsbound)
' #参数DisNum: Display quantity
' #参数rsBound: Random number Generation Range
Sub Disrndrecord (Disnum,rsbound)
DIM I,thisrnd
If Rsbound < disnum Then Disnum = Rsbound ' The total number of records is less than the number of record bars to extract
For i = 0 to DisNum-1
Thisrnd = Getrnd (rsbound) ' Gets a random number that is not repeated
Rs. Move (THISRND) ' cursor to random number position number read
Response.Write ("<br> (" &rs ("id") & ")" &rs ("Title")
Rs. Move (-THISRND)
Next
End Sub
' # function Getrnd (bound) returns a random number that is not repeated
' #参数bound: Random Range
Function Getrnd (bound)
DIM Rannum
Randomize ()
Rannum=int (BOUND*RND)
If Instr (appeared, "[" &ranNum&] ") Then ' random number generated
Rannum = Getrnd (bound)
End If
appeared = appeared & "[&ranNum&]" ' record random number that has occurred
Getrnd = Rannum
End Function
Reference: http://www.haishui.net/view.php?id=454 set Rs=server. CreateObject ("Adodb.recordset")
Sql= "SELECT * FROM table"
Rs.Open sql,conn,1,3 ' Open database table
I=1
Dim Webon ()
Do but not Rs. Eof ' Write array
Redim Preserve Webon (i)
Webon (i) =rs ("id")
I=i+1
Rs.movenext
Loop
Randomize ' randomly get records
P=int ((rs.recordcount*rnd) +1) ' generates random numbers in all records
Rndid=webon (P)
Set Rs=conn. Execute ("SELECT * from table where id=" &rndid) "opens related random records according to the array.