由於Access數據庫記錄集緩存的原因,從代碼裡得到Access數據庫隨機記錄是得不到,需要用隨機SQL語句的辦法來消除緩存。
下面就是例子:
查看例子
<@PageLanguage="C#"Debug="true">
<@importNamespace="System.Data">
<@importNamespace="System.Data.OleDb">
<title>隨機得到Access數據庫記錄</title>
<scriptrunat="server">
voidPage_Load(Objectsrc,EventArgse)
{
if(!IsPostBack)
{
stringMyConnString="Provider=Microsoft.Jet.OleDB.4.0;DataSource="
Server.MapPath("ASPxWeb.mdb.ascx");
RandomR=newRandom();
intintRandomNumber=R.Next(1,1000);
stringsql="selecttop10idAs序號,TitleAs標題fromDocumentOrderByRnd("
(-1*intRandomNumber).ToString() "*id)";
OleDbConnectionMyConnection=newOleDbConnection(MyConnString);
MyConnection.Open();
OleDbCommandcmd=newOleDbCommand(sql,MyConnection);
OleDbDataReaderdr=cmd.ExecuteReader();
DataGrid1.DataSource=dr;
DataGrid1.DataBind();
cmd.Dispose();
MyConnection.Close();
MyConnection.Dispose();
MyConnection=null;
}
}
</script>
<formrunat=server>
<ASP:DataGridHorizontalAlign="Center"
Width="600px"runat="server"Font-Size="9pt">
<AlternatingItemStyleBackColor="#EEEEEE"></AlternatingItemStyle>
<HeaderStyleBackColor="#AAAADD"Font-Bold="True"HorizontalAlign="Center"/>
</ASP:DataGrid>
</form>