首页 > 资讯列表 > 编程/数据库 >> ASP/.NET

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

ASP/.NET 2011-07-21 12:07:14 转载来源: AS网站目录整理

由于使用ADO访问Access数据库会有缓存, 内容来自中国站长资讯网(www.chinahtml.com)这在随机提取数据库数据时,例如:sql="select top 10 Title,objectGuid from Document Order By Rnd(id)",将得不到随机记录,下面的例子可以克服这一缺陷,实现

由于使用ADO访问Access数据库会有缓存, 内容来自中国站长资讯网(www.adminso.com)这在随机提取数据库数据时,例如: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">
<p>
<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>
</p>
</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">
<p>
<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>
</p>
</form>
</body>
</html>

֐??׊Ѷ͸ ֐??׊Ѷ͸

标签: ASP.NET2.0 如何 随机 读取 Access 记录


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持