国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 金门县| 宾川县| 梓潼县| 盘山县| 苗栗县| 梁山县| 桓仁| 城市| 开封市| 句容市| 绥芬河市| 德保县| 六安市| 静安区| 佛山市| 连南| 丹阳市| 新安县| 兰州市| 南汇区| 巫溪县| 日喀则市| 南康市| 吉木乃县| 绥芬河市| 济阳县| 黔西县| 永修县| 安达市| 孝义市| 凌源市| 湛江市| 天水市| 定西市| 襄汾县| 泉州市| 义乌市| 长白| 澄迈县| 姜堰市| 额尔古纳市|