由于Access数据库记录集缓存的原因,从代码里得到Access数据库随机记录是得不到,需要用随机SQL语句的办法来消除缓存。
下面就是例子:
查看例子http://dotnet.aspx.cc/Exam/GetRandom.aspx
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<title>随机得到Access数据库记录</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 序号,Title As 标题 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>
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |