Ëæ»úÏÔʾÊý¾Ý¿â¼Ç¼

Àà±ð£º.NET¿ª·¢ µã»÷£º0 ÆÀÂÛ£º0 ÍÆ¼ö£º

Ëæ»úÏÔʾÊý¾Ý¿â¼Ç¼

SystemÃû³Æ¿Õ¼äÓÐÒ»¸öRandomÀ࣬ÓÃÀ´²úÉúËæ»úÊý¡£±¾ÎľͽéÉÜÀûÓÃÕâ¸öRandomÀàÀ´Ëæ»úÏÔʾÊý¾Ý¿â¼Ç¼¡£

RandomÀàÓÐÒ»¸öÖØÔØ·½·¨½ÐNext£¬Ëü¿ÉÒÔ²úÉúËæ»úÊý£¬ËüÔÊÐíÊäÈëÁ½¸ö²ÎÊý£¬ÒÔ²úÉúÕâÁ½¸öÊýÖ®¼äµÄËæ»úÊý¡£ÀýÈ磺

Random R = new Random();
Random.Next(1,100);

½«»áÔÚ²úÉú1£­100Ö®¼äµÄËæ»úÊý¡£

ÒªËæ»úÏÔʾÊý¾Ý¿â¼Ç¼£¬ÐèÒªÖªµÀÊý¾Ý¿â×î´ó¼Ç¼ÊýºÍ×îС¼Ç¼Êý¡£

int RecNo=0,MaxRecNo,MinRecNo; Random R = new Random(); SqlDataReader DR; SqlConnection CN = newSqlConnection("Server=Mengxianhui;Database=Northwind;uid=sa");
CN.Open(); 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"] ; RecNo = R.Next(MinRecNo,MaxRecNo);

È»ºóµÃµ½Ëæ»úµÃµ½¼Ç¼¡£

Cmd = new SqlCommand("select * from Products Where ProductID = " + RecNo,CN);
DR = Cmd.ExecuteReader();
DR.Read();
Response.Write("½ñÈյIJúÆ·Ãû³Æ£º " +DR["ProductID"] + " £­ " + DR["ProductName"] + "");

CN.Close();

ÍêÕû´úÂëÈçÏ£º

<%@ Page Language="C#" Debug="true" %>
<%@Import NameSpace="System.Data.SqlClient"%>
<%@Import NameSpace="System.Data"%>
<html>
<head>
<title>Ëæ»úÏÔʾÊý¾Ý¿â¼Ç¼</title>
</head>
<body>
<script runat="server">
void Page_Load(object Sender,EventArgs E)
{
int RecNo=0,MaxRecNo,MinRecNo;
Random R = new Random();
SqlDataReader DR;
//**** Á¬½Óµ½Êý¾Ý¿â
SqlConnection CN = new SqlConnection("Server=Mengxianhui;Database=Northwind;uid=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);
//**** ÏÔÊ¾Ëæ»ú¼Ç¼ÐÅÏ¢¡£
Cmd = new SqlCommand("select * from Products Where ProductID = " + RecNo,CN);
DR = Cmd.ExecuteReader();
DR.Read();
Response.Write("½ñÈյIJúÆ·Ãû³Æ£º <b>" +DR["ProductID"] + " £­ " + DR["ProductName"] + "</b>");
DR.Close();
CN.Close();
}
</script>
</body>
</html>

±¾ÎĵØÖ·£ºhttp://com.8s8s.com/it/it45893.htm