Implementation of random Access record reading in ASP. NET 2.0

Source: Internet
Author: User

Because there is a cache when accessing the Access database using ADO, when database data is randomly extracted, for example, SQL = "select top 10 Title, objectGuid from Document Order By Rnd (id )", there will be no random records. The following example can overcome this defect and achieve random reading of the database.
C #:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
Random rnd = new Random (unchecked (int) DateTime. Now. Ticks ));
Int intRandomNumber = rnd. Next ();
String ConnectionString = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = DataDirectory aspxWeb. mdb; Persist Security Info = True ";
String SQL = "select top 10 Title, objectGuid from Document Order By Rnd (" + (-1 * intRandomNumber) + "* id )";
System. Data. OleDb. OleDbConnection cn = new System. Data. OleDb. OleDbConnection (ConnectionString );
Cn. Open ();
System. Data. OleDb. OleDbCommand cmd = new System. Data. OleDb. OleDbCommand (SQL, cn );
System. Data. OleDb. OleDbDataReader dr = cmd. ExecuteReader (System. Data. CommandBehavior. CloseConnection );
GridView1.DataSource = dr;
GridView1.DataBind ();
Dr. Close ();
Cmd. Dispose ();
Cn. Dispose ();
Cn = null;
}
</Script>

Related Article

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.