"Guide" because accessing an Access database with ADO has a cache, which is when you randomly extract database data, such as: sql= "select top Title,objectguid from Document ORDER by Rnd (ID)", Will not get the random records, the examples given in this paper can overcome this shortcoming and realize the random reading of the database.
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#:
<%@ 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>
<title> randomly read Access database records </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "GridView1" runat= "Server"
Autogeneratecolumns= "false" >
<Columns>
<asp:hyperlinkfield datanavigateurlfields= "ObjectGuid"
headertext= "article" datatextfield= "Title"
datanavigateurlformatstring=
"Http://dotnet.aspx.cc/article/{0}/read.aspx"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>