Random Read Access database records in asp.net 2.0

Source: Internet
Author: User
Tags access database

"Guide" because accessing an Access database with ADO has a cache, which is when you randomly extract database data, such as: sql= "select top Title,objectguid from Document ORDER by Rnd (ID)", Will not get the random records, the examples given in this paper can overcome this shortcoming and realize the random reading of the database.

Because accessing an Access database with ADO has a cache, this will not get random records when you randomly extract database data, such as: sql= "select top Title,objectguid from Document ORDER by Rnd (ID)." The following example can overcome this shortcoming and realize random reading of the database.

C#:

<%@ 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 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>
<title> randomly read Access database records </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "GridView1" runat= "Server"
Autogeneratecolumns= "false" >
<Columns>
<asp:hyperlinkfield datanavigateurlfields= "ObjectGuid"
headertext= "article" datatextfield= "Title"
datanavigateurlformatstring=
"Http://dotnet.aspx.cc/article/{0}/read.aspx"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
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.