Randomly extracting records from a database

Source: Internet
Author: User

There are 5 records in the database, 4 randomly selected.

Code
--------------------------------------
<%
------------------------- The database connection-----------------------
Set objconn = Server.CreateObject ("ADODB. Connection ")
Objconn.connectionstring =" provider=microsoft.jet.oledb.4.0 "&_
" Data source= "& Server.MapPath ("Data.mdb")
objConn.Open
'-------------------------database connection-----------------------

'- ------------------------Retrieve data-----------------------
strSQL = select Id,datacolumn from DataTable
' SQL statement, Retrieves the database
Set objRS = Server.CreateObject ("ADODB. Recordset ")    
Create recordset
Objrs.open strSQL, objconn, 1, 1  
Perform retrieval
count= OBJRS.RECORDCOUNT&NBSP;&NBSP;&NBSP
' Total Records received
item=4                                                     
Show number of records
-------------------------retrieve data-----------------------

'-------------------------------------------------------------------------------
ReDim A (Item, 2), T (Count)
' Define 2 arrays, array A to store records, array T to delete records

'---------------------------------------
' Initial array value, intended to be compared with this value after inserting the data
For all J in T
J=0
Next
'---------------------------------------

'---------------------------------------
' Randomly selected record number
Randomize Timer ' Initialize random number generator
For J=1 to Item
K=int (rnd*count+1) ' randomly take a record from the total
Do While T (k) <>0 ' determines whether the record is already in the array
K=int (rnd*item+1)
Loop
T (k) =1 ' K-Records selected
Next
'--------------------------------------
J=1:i=1 ' defines subscript

'--------------------------------------
' objRS Some of the records in a loop-selected dataset into an array
Do as Not objrs.eof
If T (j) =1 Then
A (i,1) =objrs ("id") ' record ID
A (i,2) =objrs ("DataColumn") ' record contents
I=i+1
End If
J=j+1
Objrs.movenext
Loop
'--------------------------------------
'-------------------------------------------------------------------------------

'----------------------------Display content--------------------
For I=1 to Item
Response.Write "Serial Number" &a (i,1) & "<br>"
Response.Write "Content" &a (i,2) & "<p>"
Next
'----------------------------Display content--------------------

'---------------------------
' Releasing resources
Objrs.close
Set objrs=nothing
Objconn.close
Set objconn=nothing
'---------------------------
%>

Data
ID DataColumn
--------------------------
1 A
2 b
3 C
4 D
5 E

SQL Server only needs newid (), but Access does not provide the NEWID () method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.