protected void Page_Load( object sender, EventArgs e) ... { dataBind(); } public void dataBind() ... { int RecNo = 0, RecNo2 = 0, MaxRecNo, MinRecNo; Random R = new Random(); SqlDataReader DR; //**** 连接到数据库 SqlConnection CN = new SqlConnection("Server=.;Database=Northwind;uid=sa;pwd=sa"); CN.Open(); //**** 找到最大的和最小的ID号 SqlCommand Cmd = new SqlCommand("select Max(ProductId) as MaxProdid ,Min(ProductId) as MinProdId from Products", CN); DR = Cmd.ExecuteReader(); DR.Read(); MaxRecNo = (int)DR["MaxProdid"]; MinRecNo = (int)DR["MinProdid"]; DR.Close(); //**** 创建一个随机数 RecNo = R.Next(MinRecNo, MaxRecNo); RecNo2 = R.Next(MinRecNo, MaxRecNo); //**** 显示随机记录信息。 Cmd = new SqlCommand("select * from Products Where ProductID = " + RecNo, CN); DR = Cmd.ExecuteReader(); DR.Read(); Response.Write("今日的产品名称: <b>" + DR["ProductID"] + " - " + DR["ProductName"] + "</b>"); //this.Label1.Text = DR["ProductID"].ToString() ; //this.Label2.Text = DR["ProductName"].ToString(); DR.Close(); CN.Close(); }