Access randomly extracts records from a database

Source: Internet
Author: User
There are 5 records in the database, and 4 are randomly selected. SQL Server only needs newid (), but Access does not provide the NEWID () method

Code
--------------------------------------
<%
'-------------------------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 statements, retrieving databases
Set objRS = Server.CreateObject ("ADODB. Recordset ")
' Create a recordset
Objrs.open strSQL, objconn, 1, 1
' Perform a search
Count=objrs.recordcount
' Get the total number of records
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



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.