国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Displaying Random Record

2024-07-21 02:22:30
字體:
供稿:網(wǎng)友
system namespace has a random class which is used for generating random numbers. this article explains how to display a random record from a database table using the random class.

the random class has an overloaded method named next which will generate random numbers. one variant of this next method takes in the minimum and maximum numbers and generates random number within the range. for example:

random r = new random();
random.next(1,100);


will generate a random number between 1 and 100.

to display a random record from the database we will pass the maximum value for the id column and minimum value for the id column to the next method and generate a random number.

int recno=0,maxrecno,minrecno;
random r =  new random();
sqldatareader dr;
sqlconnection cn = new sqlconnection("server=yourservername;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);


then we will fetch the the record whose id is equal to the random number generated.

cmd = new sqlcommand("select * from products where productid = " + recno,cn);
dr = cmd.executereader();
dr.read();
response.write("product of the day <b>" + dr["productname"] + "</b>");
cn.close();

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 博爱县| 墨江| 涡阳县| 分宜县| 夏津县| 余干县| 建阳市| 乐昌市| 广昌县| 武清区| 十堰市| 辰溪县| 海口市| 柞水县| 轮台县| 巨野县| 溧水县| 金秀| 景谷| 伊吾县| 汤阴县| 大同县| 习水县| 定远县| 凤翔县| 龙江县| 石台县| 鱼台县| 黑水县| 邯郸市| 休宁县| 乌海市| 宁津县| 蕉岭县| 广德县| 新源县| 东乡族自治县| 威宁| 威宁| 浪卡子县| 孝义市|