国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用ASP.NET2.0如何隨機(jī)讀取Access記錄?

2024-07-10 13:08:18
字體:
供稿:網(wǎng)友

由于使用ado訪問access數(shù)據(jù)庫會有緩存,這在隨機(jī)提取數(shù)據(jù)庫數(shù)據(jù)時,例如:sql="select top 10 title,objectguid from document order by rnd(id)",將得不到隨機(jī)記錄,下面的例子可以克服這一缺陷,實現(xiàn)數(shù)據(jù)庫的隨機(jī)讀取。

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>隨機(jī)讀取access數(shù)據(jù)庫記錄</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>隨機(jī)讀取access數(shù)據(jù)庫記錄</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>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 合江县| 三都| 吴堡县| 青海省| 沙湾县| 内黄县| 宝坻区| 莆田市| 枣阳市| 榆中县| 赞皇县| 镇赉县| 望都县| 兴海县| 怀化市| 磐安县| 富蕴县| 彭泽县| 乌鲁木齐市| 滁州市| 拉孜县| 靖边县| 土默特左旗| 清涧县| 石柱| 平潭县| 招远市| 天门市| 兴安县| 江门市| 宁津县| 望江县| 和田市| 清镇市| 鄢陵县| 武川县| 松阳县| 杂多县| 宣恩县| 铜梁县| 平顶山市|