Due to the cache of the Access database record set, it is not possible to obtain random records from the Access database from the code. It is necessary to use random SQL statements to eliminate the cache.
Here is an example:
View the example http://dotnet.aspx.cc/Exam/GetRandom.aspx
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<title>Get Access database records randomly</title>
<script runat="server">
void Page_Load(Object src,EventArgs e)
{
if(!IsPostBack)
{
string MyConnString = "Provider=Microsoft.Jet.OleDB.4.0;Data Source="
+ Server.MapPath("aspxWeb.mdb.ascx");
Random R = new Random();
int intRandomNumber = R.Next(1,1000);
string sql = "select top 10 id As serial number, Title As title from Document Order By Rnd("
+ (-1 * intRandomNumber).ToString() + "*id)";
OleDbConnection MyConnection = new OleDbConnection(MyConnString);
MyConnection.Open();
OleDbCommand cmd = new OleDbCommand(sql,MyConnection);
OleDbDataReader dr = cmd.ExecuteReader();
DataGrid1.DataSource = dr;
DataGrid1.DataBind();
cmd.Dispose();
MyConnection.Close();
MyConnection.Dispose();
MyConnection = null;
}
}
</script>
<form runat=server>
<asp:DataGrid id="DataGrid1" HorizontalAlign="Center"
Width="600px" runat="server" Font-Size="9pt">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center" />
</asp:DataGrid>
</form>