软讯网络 > 编程语言 > .NET > C#.NET > 随机显示数据库记录
【标 题】:随机显示数据库记录
【关键字】:
【来 源】:http://blog.csdn.net/Jon_Pilot/archive/2006/09/28/1301991.aspx
随机显示数据库记录
private void Page_Load(object sender, System.EventArgs e)
{
int RecNO, MaxRecNO, MinRecNO;
Random random = new Random();
OleDbDataReader odr;
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + Server.MapPath("data/WebFormTest.mdb");
con.Open();
OleDbCommand cmd = new OleDbCommand("select Max(ProductID) as MaxProductID,Min(ProductID) as MinProductID from Products",con);
odr = cmd.ExecuteReader();
odr.Read();
MaxRecNO = (int)odr["MaxProductID"];
MinRecNO = (int)odr["MinProductID"];
odr.Close();
RecNO = random.Next(MinRecNO, MaxRecNO);
cmd = new OleDbCommand("select * from Products where ProductID = " + RecNO,con);
odr = cmd.ExecuteReader();
odr.Read();
Response.Write("产品名称:" + odr["ProductID"] + " - " + odr["ProductName"]);
odr.Close();
con.Close();
}
【相关文章】
没有相关文章