用ASP.NET2.0如何随机读取Access记录?

access|asp.net|随机

由于使用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>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>随机读取Access数据库记录</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="objectGuid" HeaderText="文章" DataTextField="Title"
DataNavigateUrlFormatString="http://dotnet.aspx.cc/article/{0}/read.aspx" />
</Columns>
</asp:GridView>
</div>
</form>
</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.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 10 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>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>随机读取Access数据库记录</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="objectGuid" HeaderText="文章" DataTextField="Title"
DataNavigateUrlFormatString="http://dotnet.aspx.cc/article/{0}/read.aspx" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

时间: 2024-10-26 23:03:59

用ASP.NET2.0如何随机读取Access记录?的相关文章

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 

ASP.NET 2.0中随机读取Access数据库记录

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

asp.net2.0里,怎样读取一个XML文档结果的值?求求各位解说了,文档结构如下给出

问题描述 文档结构为:<?xmlversion="1.0"encoding="utf-8"?><a><aa><aaa><x>x内容</x><y>y内容</y><z>z内容</z></aaa><aaa><x>x内容</x><y>y内容</y><z>z内容</z&

Asp.net2.0中用Dataset向Access里添加记录时无值字段也会被添加为空,默认值不管用,怎么回事啊

问题描述 我用DataSet向Access里添加新记录的时候,本想省事用字段的默认值自动赋值,但现在只要OleDbDataAdapter一更新记录就将没有赋值的字段自动设置为"",字段的默认值根本不管用,这是为什么呢,请高人指点一二 解决方案 解决方案二:默认值不好用你就给他自动加个值呗!解决方案三:自己加值很麻烦,要写很多重复的代码的,所以想省事的解决方案四:沉水,自顶解决方案五:再顶解决方案六:...你在数据库里不默认null或0吗?在代码里给他等于null或0就行了.就这一句也麻

ASP.NET2.0网站的发布(高分求助)

问题描述 小弟使用vs.net2005作了个ASP.NET2.0的网站,想上传到远端服务器,现在的问题是,直接使用vs.net2005自带的打包工具制成安装文件再上传,还是使用vs.net2005开发环境下的"发布网站"功能?哪位大侠能提供有关的操作步骤(详细一点最好~嘿嘿~:) 解决方案 解决方案二:直接发布网站就可以了.然后去服务去配置一下IIS.装个.netframework2.0解决方案三:生成->发布网站上传到服务器服务器设置版本为.NETFRAMEWORK2.0解决方

ASP.NET2.0 HiddenField控件

asp.net|控件    HiddenField控件顾名思义就是隐藏输入框的服务器控件,它能让你保存那些不需要显示在页面上的且对安全性要求不高的数据.也许这个时候应该有这么一个疑问,为什么有了ViewState.Session和Cookie等状态保存机制,还需要用起HiddenField呢?增加HiddenField,其实是为了让整个状态管理机制的应用程度更加全面.因为不管是ViewState.Cookie还是Session,都有其失效的时候,比如用户因某种需求要求设置ViewState为fa

asp.net2.0加密数据库连接字符串技巧

在asp.net2.0中,发布网站时,加密web.config,这样可以有效保证数据库用户和密码安全,其步骤如下: 1.添加密钥 执行:C:WINDOWSMicrosoft.NETFrameworkv2.0.50727aspnet_regiis -pc "hnlaw" -exp 其中"hnlaw"为密钥名称 2.添加web.config节点 在web.config的<configuration></configuration>之间添加: &l

一年了,用ASP.NET2.0就没有发送成功一个邮件,我的天!

问题描述 我的代码:if(TextBox1.Text.IndexOf("@")>-1)//判断收信地址集是否有效{MailMessagemm=newMailMessage();mm.To.Add(newMailAddress(TextBox1.Text));mm.From=newMailAddress(TextBox2.Text);mm.Subject=TextBox5.Text;mm.Body=TextBox6.Text;mm.SubjectEncoding=System.Te

ASP.NET2.0+SQL Server2005构建多层应用第1/4页_基础应用

[推荐]ASP.NET2.0+SQL Server2005构建多层应用!!!!!@申请加分!@@! [sell=5]随着.NET 2.0的发布,将会使得使用ASP.NET 2.0来构建的Web应用越来越容易.使用ASP.NET 2.0和SQL Server 2005,将会比ASP.NET 1.1更方便地构建多层体系架构的web应用.本文,将使用ASP.NET 2.0和SQL Server 2005 (.net使用Visual Studio 2005 beta 2,SQL Server 2005使