How do I randomly read access records with asp.net2.0?

Source: Internet
Author: User
Tags datetime dotnet tostring xmlns access database
Access|asp.net| Random

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>

VB. NET:

<%@ Page language= "VB" debug= "true"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "Server" >
Protected Sub Page_Load (ByVal sender as Object, ByVal e as System.EventArgs)
Dim timestring as String = DateTime.Now.Ticks.ToString ()
Dim A as UInt32 = Uint32.parse (timestring.substring (timestring.length-8, 8))
Dim B as Int32 = Bitconverter.toint32 (Bitconverter.getbytes (a), 0)
Dim rnd as Random = New Random (b)
Dim Intrandomnumber as Integer = rnd. Next
Response.Write (Intrandomnumber)
Dim ConnectionString as String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source= datadirectory aspxweb.mdb;" Persist Security Info=true "
Dim SQL as String = "SELECT top Title,objectguid from Document order by Rnd (" + ( -1 * intrandomnumber). ToString () + "*id"
Dim cn as System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection
(ConnectionString)
cn. Open ()
Dim cmd as System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand (SQL, CN)
Dim dr as System.Data.OleDb.OleDbDataReader = cmd. ExecuteReader (System.Data.CommandBehavior.CloseConnection)
Gridview1.datasource = Dr
Gridview1.databind ()
Dr. Close ()
Cmd. Dispose ()
cn. Dispose ()
cn = Nothing
End Sub
</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.