<%@ 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();