Because there is a cache when accessing the Access database using ADO, when database data is randomly extracted, for example, SQL = "select top 10 Title, objectGuid from Document Order By Rnd (id )", there will be no random records. The following example can overcome this defect and achieve random reading of the database.
C #:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
Random rnd = new Random (unchecked (int) DateTime. Now. Ticks ));
Int intRandomNumber = rnd. Next ();
String ConnectionString = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = DataDirectory aspxWeb. mdb; Persist Security Info = True ";
String SQL = "select top 10 Title, objectGuid from Document Order By Rnd (" + (-1 * intRandomNumber) + "* id )";
System. Data. OleDb. OleDbConnection cn = new System. Data. OleDb. OleDbConnection (ConnectionString );
Cn. Open ();
System. Data. OleDb. OleDbCommand cmd = new System. Data. OleDb. OleDbCommand (SQL, cn );
System. Data. OleDb. OleDbDataReader dr = cmd. ExecuteReader (System. Data. CommandBehavior. CloseConnection );
GridView1.DataSource = dr;
GridView1.DataBind ();
Dr. Close ();
Cmd. Dispose ();
Cn. Dispose ();
Cn = null;
}
</Script>