由于使用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>
新闻热点
疑难解答
图片精选