数据库分页,求一个最简单的读取数据并分页

求一个最简单的读取数据并分页 - 故障解答 - 电脑教程网

求一个最简单的读取数据并分页

日期:2007-03-03   荐:
求一个最简单的读取数据并分页数据库里的 表是 wenzhang1 , 字段名为 newstitle .怎么读取newstitle里的N多数据并分页显示.thanks很全的一个分页:<% const maxperpage=15 dim listsql,sql,lastsql,totalput,currentpage,i dim strqy_name,straddress,strqy_fr,strqy_fzrq strqy_name=request.Form("qy_name") straddress=request.Form("address") strqy_fr=request.Form("qy_fr") strqy_fzrq=request.Form("qy_fzrq") if straddress="请选择" then straddress="" end if sql="select * from zz_info" listsql="select * from zz_info" if strqy_name <> "" then listsql=listsql&" where qy_name like '%"& strqy_name &"%'" end ifif straddress <> "" then if sql<>listsql then listsql=listsql&" and address like '%"& straddress &"%'" else listsql=listsql&" where address like '%"& straddress &"%'" end if end if if strqy_fr <> "" then if sql<>listsql then listsql=listsql&" and qy_fr like '%"& strqy_fr &"%'" else listsql=listsql&" where qy_fr like '%"& strqy_fr &"%'" end if end if if strqy_fzrq <> "" then if sql<>listsql then listsql=listsql&" and qy_fzrq='"& strqy_fzrq &"'" else listsql=listsql&" where qy_fzrq='"& strqy_fzrq &"'" end if end if lastsql=listsql&" order by qy_fzrq desc" rs.open lastsql,conn,1,1 rs.pagesize=maxperpage if trim(request("page"))<>"" then currentpage=CLng(request("page")) if currentpage>rs.pagecount then currentpage=rs.pagecount end if else currentpage=1 end if if rs.Eof then response.write"对不起!没有你要找的任何记录!" response.end else totalput=rs.recordcount if currentpage<>1 then if (currentpage-1)*maxperpage<totalput then rs.move(currentpage-1)*maxperpage dim bookmark bookmark=rs.bookmark end if end if dim n,k if (totalput mod maxperpage)=0 then n=totalput\maxperpage else n=totalput\maxperpage 1 end if %> <font color="#006600">第<%=currentpage%>页/共<%=n%>页----总计<%=rs.recordcount%>个企业信息</font> <% k=currentpage if k<>1 then response.write"[<b>" "<a href=zz_info_update.asp?page=1&qy_name="& strqy_name &"&address="& straddress &"&qy_fr="& strqy_fr &"&qy_fzrq="& strqy_fzrq &">第一页</a></b>]" response.write"[<b>" "<a href=zz_info_update.asp?page=" cstr(k-1) "&qy_name="& strqy_name &"&address="& straddress &"&qy_fr="& strqy_fr &"&qy_fzrq="& strqy_fzrq &"></a></b>]" else response.write"[第一页][]" end if if k<>n then response.write"[<b>" "<a href=zz_info_update.asp?page=" cstr(k 1) "&qy_name="& strqy_name &"&address="& straddress &"&qy_fr="& strqy_fr &"&qy_fzrq="& strqy_fzrq &"></a></b>]" response.write"[<b>" "<a href=zz_info_update.asp?page=" cstr(n) "&qy_name="& strqy_name &"&address="& straddress &"&qy_fr="& strqy_fr &"&qy_fzrq="& strqy_fzrq &">最后一页</a></b>]" else response.write"[][最后一页]" end if%><table width="100%" border="1" cellspacing="1" cellpadding="0"> <tr bgcolor="#9C0000" class="style1"> <td width="12%" nowrap><div align="center">执照注册编号</div></td> <td width="22%" nowrap><div align="center">企业名称</div></td> <td width="23%" nowrap><div align="center">法人所在</div></td> <td width="11%" nowrap><div align="center">企业法人</div></td> <td width="9%" nowrap><div align="center">经济性质</div></td> <td width="9%" nowrap><div align="center">经营方式</div></td> <td width="14%" nowrap><div align="center">注册日期</div></td> </tr> <% i=0 Do until (rs.EOF or i>=maxperpage) if i mod 2 =0 then response.Write"<tr bgcolor=#efefda>" else response.Write"<tr bgcolor=#d5f3f4>" end if %> <td nowrap><div align="center"><a href="zz_info_update_wirte.asp?zz_id=<%=trim(rs("zz_id"))%>"><%=trim(rs("zz_id"))%></a></div></td> <td nowrap><div align="center"><%=trim(rs("qy_name"))%></div></td> <td nowrap><div align="center"><%=trim(rs("address"))%></div></td> <td nowrap><div align="center"><%=trim(rs("qy_fr"))%></div></td> <td nowrap><div align="center"><%=trim(rs("qy_xz"))%></div></td> <td nowrap><div align="center"><%=trim(rs("qy_way"))%></div></td> <td nowrap><div align="center"><%=trim(rs("qy_fzrq"))%></div></td> </tr> <% i=i 1 rs.MoveNext Loop end if rs.close %>而且还是多条件查询的!CREATE PROCEDURE dbo.picdisplay @foldernum int,@id int,@page int,@totalpage int output ASdeclare @thepage intdeclare @pagenum intdeclare @sql nvarchar(200)set @thepage=(@page-1)*4if @foldernum=0 beginset @sql='select top 4 * from pic where id=' cast(@id as nvarchar(30)) 'and thepicid not in (select top ' cast(@thepage as nvarchar(30)) ' thepicid from pic where id=' cast(@id as nvarchar(30)) ' order by thepicid) order by thepicid'exec sp_executesql @sqlselect @pagenum=count(*) from pic where id=@idselect @totalpage=@pagenum/4 1end elseif @foldernum=1beginset @sql='select top 4 * from pic where id=' cast(@id as nvarchar(30)) 'and share=1 and thepicid not in (select top ' cast(@thepage as nvarchar(30)) ' thepicid from pic where id=' cast(@id as nvarchar(30)) ' and share=1 order by thepicid) order by thepicid'exec sp_executesql @sqlselect @pagenum=count(*) from pic where id=@id and share=1select @totalpage=@pagenum/4 1end elseif @foldernum=2beginset @sql='select top 4 * from pic where id=' cast(@id as nvarchar(30)) 'and foldernum=0 and thepicid not in (select top ' cast(@thepage as nvarchar(30)) ' thepicid from pic where id=' cast(@id as nvarchar(30)) ' and foldernum=0 order by thepicid) order by thepicid'exec sp_executesql @sqlselect @pagenum=count(*) from pic where id=@id and foldernum=0select @totalpage=@pagenum/4 1end elsebeginset @sql='select top 4 * from pic where id=' cast(@id as nvarchar(30)) 'and foldernum=' cast(@foldernum as nvarchar(30)) ' and thepicid not in (select top ' cast(@thepage as nvarchar(30)) ' thepicid from pic where id=' cast(@id as nvarchar(30)) ' and foldernum=' cast(@foldernum as nvarchar(30)) ' order by thepicid) order by thepicid'exec sp_executesql @sqlselect @pagenum=count(*) from pic where id=@id and share=1select @totalpage=@pagenum/4 1endGO<%'================= ProgrammName: Cutpage ======================='================ Programmed by Zwan =====================Function cutPage(sqlStr,Conn,dateNums,pageNums,URLs)'利用ADO分页的函数 Dim sql,Cn,dateNum,pageNum,URL,rsDate Sql=Trim(sqlStr) '获得sql语句。 Set Cn=Conn '获得数据对象 dateNum=Cint(dateNums) '获得每页得记录数4 pageNum=Cint(pageNums) '获得当前页码 URL=Trim(URLs) '获得路径如test.asp或test.asp?page=2 Set rsDate=Server.CreateObject("ADODB.Recordset") rsDate.PageSize=dateNum'将当前页传给每页大小 rsDate.Open Sql,cn,1,1 IF rsDate.Eof Then Response.Write("<center><font stlye=&font-size:14px; & color=&#ff0000&>对不起,没有记录!</font></center>") Else IF pageNum="" or pageNum<1 Then pageNum=1 ElseIf pageNum>rsDate.PageCount Then pageNum=rsDate.PageCount End IF rsDate.absolutepage =pageNum Dim recordHead,recordLast '定义当前页开头记录和结束记录 recordHead=1 If pageNum>1 Then recordHead=dateNum*(pageNum-1)'开头记录等于当前页数减一乘以每页数 If pageNum>=rsDate.PageCount Then recordLast=rsDate.RecordCount Else recordLast=dateNum*pageNum'结束记录等于当前页数乘以每页数 End If Response.Write("<table width=100% border=&0& cellpadding=&0& cellspacing=&0& style=&font-size:12px;&>") Response.Write("<tr><td height=38 align=left><font stly=&font-size:12px;&>(第"&recordHead&"-"&recordLast&"条,共"&rsDate.Recordcount&"条,每页显示"&dateNum&"条)</font></td></tr>") Dim URLa '定义判断输入得URL包含?没有的变量 Dim upPage,downPage,allPage '定义向上和向下翻的变量 Dim allWrite '定义输出 upPage=pageNum-1 downPage=pageNum 1 URLa=Split(URL,"?",-1,1) If URLa(0)=URL Then upPage="<a href=" & URL & "?page=" & upPage &" stlye=&font-size:12px;&></a> " If pageNum=1 Then upPage="" downPage="<a href=" & URL & "?page=" & downPage &" stlye=&font-size:12px;&></a>" If pageNum=rsDate.PageCount Then downPage="" Else upPage="<a href=" & URL & "&page=" & upPage &" stlye=&font-size:12px;&></a> " If pageNum=1 Then upPage="" downPage="<a href=" & URL & "&page=" & downPage &" stlye=&font-size:12px;&></a>" If pageNum=rsDate.PageCount Then downPage="" End If allWrite=upPage & downPage & " 共" & rsDate.PageCount & "页 " & "目前第"& pageNum &"页" allwrite="<font style=&font-size:12px;&>" & allWrite & " 到第<input type=&text& style=&width:30px;& Name=&page&>页<input type=submit value=GO></font>" Response.Write("<tr><form name=&formPage& method=&post& action="&URL&"><td height=30 align=right style=&font-szie=12px;&>" & allWrite & "</td></form></tr>") Response.Write("<tr><td align=center>") Response.Write("<table width=100% style=&font-size:12px;&>")'设置内置表格的属性 Response.Write("<tr>") Response.Write("<tr bgcolor=#97CFD9 height=25>") Dim id,i For i=0 to rsDate.Fields.Count-1 '设置表头 Response.Write("<td align=&center&><font style=&font-size:14px;&><b>"&rsDate.Fields(i).Name&"</b></font></td>")'输出Filed名如需输出中文名,可分N次输出N个表头名 'Response.Write("<td align=&center&><font style=&font-size:14px;&><b>姓名</b></font></td>") 'Response.Write("<td align=&center&><font style=&font-size:14px;&><b>用户名</b></font></td>") 'Response.Write("<td align=&center&><font style=&font-size:14px;&><b>密码</b></font></td>") 'Response.Write("<td align=&center&><font style=&font-size:14px;&><b>邮箱</b></font></td>") '....... '....... '....... Next Response.Write("</tr>") id=0 While not rsDate.EOF and id<dateNum id=id 1'隔行输出的表单的背景色不同,单行与双行 If id Mod 2=0 then Response.Write("<tr bgcolor=#cccccc>")'双行 Else Response.Write("<tr bgcolor=#999999>")'单行 End If For Each fils in rsDate.Fields Response.Write("<td align=&center& height=20>"&fils&"</td>") Next Response.Write("</tr>") rsDate.MoveNext Wend Response.Write("<tr height=25 bgcolor=#97CFD9>") For i=0 to rsDate.Fields.Count-1 '设置表尾 Response.Write("<td align=&center&><font style=&font-size:14px;&><b>"&rsDate.Fields(i).Name&"</b></font></td>")'同上,如表头输出一样,也可以不要 Next Response.Write("</tr>") Response.Write("</table></td></tr>") Response.Write("<tr><form name=&formPage1& method=&post& action="&URL&"><td height=30 align=right>" & allWrite & "</td></form></tr>")'再次输出表单,在表格下面同上 End IF rsDate.close Set rsDate=nothingEnd Function'如下对函数作一些补充'可对其中的每一个字段的每一个名作链接'可以更改CSS'可以加如[1,2,3,4,5,6]之些的链接%><%'下面为实际应用%><!--#include file=conn.asp--><%page=request("page") sql="select * from gs order by id desc" '写sql语句url="test1.asp"Call cutPage(sql,conn,"4",page,url)'3代表每页显示3条记录%>谢谢楼上的几位朋友,自己学写了个但是怎么不分页呢?<%sql="select * from down order by id desc"set rs=server.createobject("adodb.recordset")rs.open sql,conn,1,1%><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title><%=now()%></title></head><body><table width="100%"><%const maxperpage=20dim currentpagers.pagesize=maxperpagecurrentpage=request.querystring("page")if currentpage="" thencurrentpage=1elseif currentpage<1 thencurrentpage=1elsecurrentpage=clng(currentpage)if currentpage > rs.pagecount thencurrentpage=rs.pagecountend if end ifif not isnumeric(currentpage) thencurrentpage=1end ifdim totalput,ntotalput=rs.recordcountif totalput mod maxperpage=0 thenn=totalput\maxperpageelsen=totalput\maxperpage 1end ifif n=0 thenn=1end ifrs.move(currentpage-1)*maxperpagei=0do while i< maxperpage and not rs.eof%><tr><td><ul><li><a href="http://soft.ttwe.com/soft/<%=rs("id")%>.asp"><%=rs("cxn")%></li></ul></a></td></tr><% rs.movenext loop 'end if %></table> <table width="100%"> <tr> <td align="center">页数<%=currentpage%>/<% =n%> <%k=currentpageif k<>1 then%><a href="list.asp?page=1">首页</a> <a href="list.asp?page=<%=k-1%>">上页</a> <%else%> 首页 上页 <%end if%><%if k<>n then%><a href="list.asp?page=<%=k 1%>">下页</a> <a href="list.asp?page=<%=n%>">尾页</a> <%else%> 下页 尾页 <%end if%>总共有<%=totalput%>条数据</td></tr></table></body></html>
标签: