ASP随机读取数据库中几条数据代码
分类:程序开发 Tags: asp 数据库 评论:0 浏览:5478 最近更新时间:2008/5/22 23:42:34
方法一:
Randomize(Timer())
SQL = "SELECT TOP 100 ID FROM table1 ORDER BY Rnd(-(ID + " & Int((10000 * Rnd) + 1) & "))"
Set RS = Server.CreateObject("ADODB.RecordSet")
RS.Open SQL,Conn,1,1
If Not RS.EOF Then
RS.MoveFirst
For I = 1 To RS.RecordCount
Response.Write RS(0) & "<br>"
RS.MoveNext
Next
End If
RS.Close
Set RS = Nothing
方法二:
’--- 连接数据库
Dim Db,Conn
Db = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("date/shop.mdb")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open Db
’--- ASP&Access随机读取某段数据
Randomize
’--- 得到总得记录数
dim countid
countid=conn.execute("select count(id) from product")(0)
’--- 获得随机数
dim rndid
rndid=int(rnd*countid)
rndid=rndid+5
dim rs,sql
’--- SQL查询
sql="select top 5 id,name from product where id in (select top "&rndid&" id from product) order by id desc"
’Response.Write(sql)
set rs=conn.execute(sql)
if not rs.eof then
do while not rs.eof
response.Write(rs(0)&" - <a href=""list.asp?id="&rs(0)&""" target=""_blank"">"&rs(1)&"</a><BR />")
rs.movenext
loop
end if
set rs=nothing
Randomize(Timer())
SQL = "SELECT TOP 100 ID FROM table1 ORDER BY Rnd(-(ID + " & Int((10000 * Rnd) + 1) & "))"
Set RS = Server.CreateObject("ADODB.RecordSet")
RS.Open SQL,Conn,1,1
If Not RS.EOF Then
RS.MoveFirst
For I = 1 To RS.RecordCount
Response.Write RS(0) & "<br>"
RS.MoveNext
Next
End If
RS.Close
Set RS = Nothing
方法二:
’--- 连接数据库
Dim Db,Conn
Db = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("date/shop.mdb")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open Db
’--- ASP&Access随机读取某段数据
Randomize
’--- 得到总得记录数
dim countid
countid=conn.execute("select count(id) from product")(0)
’--- 获得随机数
dim rndid
rndid=int(rnd*countid)
rndid=rndid+5
dim rs,sql
’--- SQL查询
sql="select top 5 id,name from product where id in (select top "&rndid&" id from product) order by id desc"
’Response.Write(sql)
set rs=conn.execute(sql)
if not rs.eof then
do while not rs.eof
response.Write(rs(0)&" - <a href=""list.asp?id="&rs(0)&""" target=""_blank"">"&rs(1)&"</a><BR />")
rs.movenext
loop
end if
set rs=nothing
本文地址:/342/
-
• ASP Access数据库连接关闭与.ldb文件锁定解决方法
• 中国十大ASP CMS系统介绍
• MySQL报Field‘***’doesn’t have a default value错误的解决办法
• UEditor百度编辑器在html代码模式无法更新内容的解决办法
• ASP在线创建ACCESS表或添加字段代码
• Fckeditor 2.6.3 ASP版增加检测上传文件是否为木马功能
• 兼容IE和firefox火狐浏览器的网页两侧对联广告代码
• 使用Aspjpeg组件,自动按宽高比例生成产品缩略图ASP代码
• 301重定向代码(ASP,ASP.NET,PHP,apache,IIS,ISAPI_Rewrite)
• ASP如何获取当前网站地址,网页地址代码及div+css菜单应用