Random display database record

xiaoxiao2021-03-05  20

The SYSTEM namespace has a Random class that generates random number. This article introduces this Random class to randomize database records. The Random class has a heavy-load method called next, which can generate random numbers, which allows two parameters to generate random numbers between the two numbers. For example: random r = new random (); random.next (1,100); will be random number between 1-100. To randomize database records, you need to know the maximum number of records and minimum records of the database. 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 ("SELECTID) AS MAXPRODID, Min (Productid) AS MinProdiDID, CN); DR = cmd.executeReader (); Dr.Read (); maxRecno = (int) DR [" MAXPRODID "]; MinRecno = (int) DR ["minprodid"]; recno = r.next (minRecno, maxRecno); then get randomly logged. CMD = New SQLCOMMAND ("Select * from products where product); DR = cmd.executeReader (); Dr.Read (); response.write (" Today's product name: " DR [" ProductID "] " - " " "ProductName"] "" "; cn.close (); complete code as follows: <% @ page language =" c # "debug =" true "%> <% @ import namespace = "System.Data.sqlclient"%> <% @ Import namespace = "system.data"%>

Random display database record