Because accessing an Access database with ADO has a cache, this will not get random records when you randomly extract database data, such as: sql= "select top Title,objectguid from Document ORDER by Rnd (ID)." The following example can overcome this shortcoming and realize random reading of the database.
C#:
Copy Code code 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 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>