access| Solution | random | show | Read a lot of people talk about access to random records of posts, no, write a random display of records to solve, I hope you correct me. There are 5 records in the database, and 4 are randomly selected.
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-----------------------'------------------------- Retrieving data-----------------------strSQL = "SELECT id,datacolumn from DataTable" SQL statement, retrieving database Set objRS = Server.CreateObject (" ADODB. Recordset ") ' Create recordset Objrs.open strSQL, objconn, 1, 1 ' Perform search Count=objrs.recordcount ' get total number of records item=4 ' Show the number of records '-------------------------retrieve data-----------------------'--------------------------- ----------------------------------------------------ReDim A (Item, 2), T (Count) ' defines 2 arrays, array A is used to store records, The array t is used to delete the record '---------------------------------------' initial array value, which is intended to be compared to this value after inserting the data for the each J-T j=0 Next '---------------------------------------'---------------------------------------' randomly extract 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. ' While T (k) <>0 ' determines whether the record is already The K=int (rnd*item+1) loop T (k) =1 ' K ' in the array is selected Next '--------------------------------------j=1:i=1 ' define subscript '--------- -----------------------------' Loop Select DataSet objRS part of the record in the array do objrs.eof if T (j) =1 then A (i,1) =objrs ("id") ' Record ID A (i,2) =objrs ("DataColumn") ' Records content i=i+1 End If j=j+1 objrs.movenext Loop '------------------------------- -------'-------------------------------------------------------------------------------'----------------------- -----Display Content--------------------for I=1 to Item Response.Write "Ordinal" &a (i,1) & "<br>" Response.Write "Content" & A (i,2) & "<p>" Next----------------------------display content--------------------'---------------------------' Releasing resources Objrs.closeset Objrs=nothingobjconn.closeset objconn=nothing '---------------------------%>
Data
ID DataColumn--------------------------1 A2 b3 C4 d5 E