Random Read Access database records in asp.net 2.0

Source: Internet
Author: User
Tags datetime dotnet xmlns access database

<%@ 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) + &am P;quot;*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> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title> randomly read Access database records </title> </head> <body> <form id="form1" Runat="server"> <div> <asp:gridview id=" gridview1" runat="server" Autogeneratecolumns="false"> < Columns> <asp:hyperlinkfield datanavigateurlfields="objectguid" headertext=" Articles " datatextfield=" title" datanavigateurlformatstring="http://dotnet.aspx.cc/article/{0}/read.aspx" /> </Columns> </asp:GridView> </div> </form& gt; </body> </html>

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.to Int32 (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 () +; quot;*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> <html xmlns="http://www.w3.org/1999/xhtml"> <head id=" head1" Runat="server"> <title> randomly read Access database records </title> </head> <body> <form id="form1" Runat="server"> <div> <asp:gridview id=" gridview1" runat="server" Autogeneratecolumns="false"> < Columns> <asp: HyperLinkField datanavigateurlfields="objectguid" headertext=" Articles " datatextfield=" title" datanavigateurlformatstring="http://dotnet.aspx.cc/article/{0}/read.aspx" /> </Columns> </asp:GridView> </div> </form> </body> </html>

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.