国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 西充县| 宽城| 黄骅市| 安阳市| 长寿区| 淮南市| 许昌市| 彰化县| 兴业县| 衡阳市| 正定县| 江安县| 吉木乃县| 铜山县| 商河县| 东平县| 长葛市| 吉水县| 龙海市| 扎鲁特旗| 武安市| 金昌市| 泗阳县| 响水县| 外汇| 紫阳县| 利津县| 达日县| 上饶市| 巴里| 抚州市| 乐山市| 兰坪| 定南县| 吉木乃县| 曲阳县| 炉霍县| 九江县| 阿拉善左旗| 越西县| 永吉县|