• ASP.NET 2.0随机读取Access记录


    由于使用ADO访问Access数据库会有缓存,这在随机提取数据库数据时,例如:sql="select top 10 Title,objectGuid from Document Order By Rnd(id)",将得不到随机记录,下面的例子可以克服这一缺陷,实现数据库的随机读取。

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

  • 相关阅读:
    字符串的长度 -- js
    导入drupal中文语言包
    ubuntu修改iP地址
    人生需要苦难和敌人
    Drupal.theme未解之谜
    如何定义带下标的js数组
    smtp admin email 似乎可以考虑在
    js中的apply 和 call
    js 点号 中括号
    代码调试
  • 原文地址:https://www.cnblogs.com/fubeidong/p/622957.html
Copyright © 2020-2023  润新知