Asp. NET randomly displaying database records

Source: Internet
Author: User
asp.net| Data | database | random | Display the System namespace has a random class that is used to generate random numbers. This article introduces the use of this random class to randomly display database records.

The random class has an overloaded method called Next, which produces random numbers that allow two parameters to be entered to produce a random number between these two numbers. For example:

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

The random number that will be generated between 1-100.

To randomly display a database record, you need to know the maximum number of records and the minimum number of records.

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);

And get a random record.

CMD = new SqlCommand ("SELECT * from the products Where ProductID =" + RECNO,CN);
DR = Cmd.executereader ();
DR. Read ();
Response.Write ("Today's product Name:" +dr["ProductID"] + "-" + dr["ProductName"] + "");
CN. Close ();

The complete code is as follows:
<%@ Page language= "C #" debug= "true"%>
<% @Import namespace= "System.Data.SqlClient"%>
<% @Import namespace= "System.Data"%>
<title> randomly display database records </title>
<body>
<script runat= "Server" >
void Page_Load (Object Sender,eventargs E)
{
int recno=0,maxrecno,minrecno;
Random R = new Random ();
SqlDataReader DR;
Connecting to a database
SqlConnection CN = new SqlConnection ("Server=mengxianhui;database=northwind;uid=sa");
CN. Open ();
Find the largest and smallest ID number
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 ();
Create a random number
Recno = R.next (MINRECNO,MAXRECNO);
Displays random record information.
CMD = new SqlCommand ("SELECT * from the products Where ProductID =" + RECNO,CN);
DR = Cmd.executereader ();
DR. Read ();
Response.Write ("Today's product Name: <b>" +dr["ProductID"] + "-" + dr["ProductName"] + "</b>");
DR. Close ();
CN. Close ();
}
</script>
</body>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.