注册 登录
编程论坛 ASP技术论坛

do while与for循环的区别

ab382717036 发布于 2011-03-30 15:14, 4657 次点击
这里我用for循环页面就出现
错误类型:
ADODB.Field (0x80020009)
BOF 或 EOF 中有一个是“真”,或者当前的记录已被删除,所需的操作要求一个当前的记录。
/人才招聘系统asp.0602/page.asp
而用Do while 循环就能实现分页功能
<!--#include file="Conn.asp" -->
<!--#include file="Comm/Site_Class.asp" -->
<%
PagePath = ""

Set Qsite = New Site_Class

Qsite.SiteConfig_Master()
%>
<%
Dim Rs,Sql,i,CurrentPage,FileName,j
Dim PersonName,ViewType
Dim PerId
Dim Act,IsHidden,IsLock,IsGj,IsTj,IsYear,IsSh
Dim TodayDate
Set Rs = server.CreateObject("ADODB.recordset")

%>
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>简历管理</title>
<link href="../skins/css/main.css" rel="stylesheet" type="text/css">


<body>
<table width="100%" border="0" cellspacing="1" cellpadding="5">
    <tr>
        <td width="8%" class="td_content"><strong>用户名</strong></td>
        <td width="8%" class="td_content"><strong>真实姓名</strong></td>
        <td width="6%" class="td_content"><strong>性别</strong></td>
        <td width="6%" class="td_content"><strong>年龄</strong></td>
        <td width="8%" class="td_content"><strong>学历</strong></td>
        <td width="12%" class="td_content"><strong>求职意向</strong></td>
    </tr>
<%
Rs.Open "select * from PersonTbl order by EditDate desc", Conn , 1 , 1
If Rs.Eof Then
%>
    <tr>
        <td height="45" colspan="11" class="td_content">暂无简历信息</td>
    </tr>
<%
Else
    CurrentPage=Clng(Request("page"))
    Rs.PageSize=15
    if CurrentPage="" Then CurrentPage=1
    if isNumeric(CurrentPage) Then CurrentPage=int(CurrentPage)
    if CurrentPage<1 Then CurrentPage=1
    if CurrentPage>rs.pagecount  Then
       CurrentPage=rs.pagecount
     End if
     if CurrentPage<=rs.pagecount and rs.pagecount>0 Then
     rs.absolutepage=CurrentPage
     End if
    i = 0
    if not rs.Eof Then  ------------
     for j=1 to rs.recordcount-----------改为DO While not rs.Eof
%>
    <tr>
        <td class="td_content"><%=Rs("UserName")%></td>
        <td class="td_content"><%=Rs("RealName")%></a></td>
        <td class="td_content"><%=Rs("Sex")%></td>
        <td class="td_content"><%=Rs("Age")%></td>
        <td class="td_content"><%=Rs("Qualification")%></td>
        <td class="td_content"><%=Rs("JobPost")%></td>
<%
    i = i + 1
    response.Write(i)
    response.Write(j)
    response.Write("<br>")
    Rs.MoveNext
    If i >= Rs.PageSize Then Exit For-------For 改为Do
    next------------------------------------Loop
End if
End If
%>
</table>
<%If Rs.pagecount>1 Then%>
<% If CurrentPage > 1 Then %>
<a href="page.asp?page=<%=CurrentPage-1%>">前一页</a>&nbsp;
  <% Else %>
  首页 上页
  <% End if%>
  <%  If CurrentPage < Rs.pagecount Then %>
<a href="page.asp?page=<%=CurrentPage+1%>">后一页</a>&nbsp;
<% End if%>
<a href="page.asp?page=<%=rs.pagecount%>">末页</a>
<% End if%>
</p>
<p align="center">现在是第<%=CurrentPage%>页一共有<%=rs.pagecount%>页</p>
<%
Rs.Close
%>

</body>
</html>
13 回复
#2
yms1232011-03-30 15:34
程序代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.
>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!--#include file="CONDB.inc"-->
<!--引用数据库连接文件-->
<%
   
'声明数据库记录集对象
   Dim rsano
   
Dim oWebI
   
'创建数据库记录集对象
   Set rsano=Server.CreateObject("ADODB.Recordset")
   
'设定数据库记录集游标类型
   rsano.cursortype=3
   
'打开记录集对象
   rsano.Open "select * From ANOUCE ORDER BY AnnounceTheDate DESC",con
   
'设定每页显示记录数
   rsano.pagesize=25
   
'得到请求的页号
   page=clng(request("page"))
   
IF request("page")<>"" then
     page
=clng(request("page"))
   
Else
     page
=1
   
End IF
   
IF page<1 Then page=1
   
If page>rsano.pagecount Then page=rsano.pagecount
   rsano.absolutepage
=page
%>
<title>网站公告页面</title>
<!--去除超连接下划线样式表和字体大小设定代码-->
<style type="text/css">
<!--
  <%=PageStyleT.StyleString%>
-->
</style>
<STYLE type=text/css>
A
{
    TEXT-TRANSFORM
: none; TEXT-DECORATION: none
}
UNKNOWN
{
    TEXT-DECORATION
: underline
}
.style22
{font-size: 12px}
.style16
{font-size: 14px}
</STYLE>
</head>

<BODY>

<table width="294" height="46" border="0" cellspacing="0">
  <tr>
    <td width="338"><div align="center" class="style22">公告标题</div></td>
    <td width="95"><div align="center" class="style22">发布日期</div></td>
  </tr>
  <%
  
'定义循环变量
  dim iPage
  
For iPage=1 to rsano.Pagesize
   
'recno=(page-1)*rsano.pagesize+ipage记录编号计算公式
   Response.Write("<tr>")
     Response.write(
"<td><span class=style16><a href=announceview.asp?anbh="&rsano.Fields(0).Value&" target=_self>"&rsano.Fields(1).Value&"</a></span></td>")
     Response.Write(
"<td><span class=style22>"&FormatDateTime(rsano.Fields(4).Value,2)&"</span></td>")
   Response.Write(
"</tr>")
   rsano.MoveNext
   
IF rsano.EOF Then
      
Exit For
   
End If
  
Next
  
%>
</table>
<table width="293" border="0">
  <tr>
  <%
  
'翻页控制代码
  IF page<>1 Then
    Response.Write(
"<td><span class=style22><a href=announce.asp?page=1>第一页</a></span></td>")
    Response.Write(
"<td><span class=style22><a href=announce.asp?page="&(page-1)&">上一页</a></span></td>")
  
Else
    Response.Write(
"<td><span class=style22>第一页</span></td>")
    Response.Write(
"<td><span class=style22>上一页</span></td>")
  
End IF   
  
IF page<>rsano.pagecount then
    Response.Write(
"<td><span class=style22><a href=announce.asp?page=" & (page+1) & ">下一页</a></span></td>")
    Response.Write(
"<td><span class=style22><a href=announce.asp?page=" & rsano.pagecount & ">最后一页</a></span></td>")
  
Else
    Response.Write(
"<td><span class=style22>下一页</span></td>")
    Response.Write(
"<td><span class=style22>最后一页</span></td>")
  
End IF
  
%>
    <td><span class=style22><%=rsano.pagecount%></span></td>
    <td><span class=style22>当前第<%=page%></span></td>
  </tr>
</table>
<%
'网页对象析构代码
rsano.Close
con.Close
Set rsano=Nothing
Set con=Nothing
%>
</body>
</html>
ASP的简单分页代码
#3
ab3827170362011-03-30 15:56
我只想知道我那种情况 for循环为什么不行,而Do while 循环可以
#4
dzt00012011-03-30 18:21
是你写的代码不对,你去查一下如何用for next分页
#5
hams2011-03-31 08:07
for是无论条件成立不成立都至少执行一次,如果条件不符造成的空子集,自然会出错。
#6
ab3827170362011-03-31 08:53
<!--#include file="Conn.asp" -->
<!--#include file="Comm/Site_Class.asp" -->
<%
PagePath = ""

Set Qsite = New Site_Class

Qsite.SiteConfig_Master()
%>
<%
Dim Rs,Sql,i,CurrentPage,FileName,j
Dim PersonName,ViewType
Dim PerId
Dim Act,IsHidden,IsLock,IsGj,IsTj,IsYear,IsSh
Dim TodayDate
Set Rs = server.CreateObject("ADODB.recordset")

%>
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>简历管理</title>
<link href="../skins/css/main.css" rel="stylesheet" type="text/css">


<body>
<table width="100%" border="0" cellspacing="1" cellpadding="5">
    <tr>
        <td width="8%" class="td_content"><strong>用户名</strong></td>
        <td width="8%" class="td_content"><strong>真实姓名</strong></td>
        <td width="6%" class="td_content"><strong>性别</strong></td>
        <td width="6%" class="td_content"><strong>年龄</strong></td>
        <td width="8%" class="td_content"><strong>学历</strong></td>
        <td width="12%" class="td_content"><strong>求职意向</strong></td>
    </tr>
<%
Rs.Open "select * from PersonTbl order by EditDate desc", Conn , 1 , 1
If Rs.Eof Then
%>
    <tr>
        <td height="45" colspan="11" class="td_content">暂无简历信息</td>
    </tr>
<%
Else
    CurrentPage=Clng(Request("page"))
    Rs.PageSize=15
    if CurrentPage="" Then CurrentPage=1
    if isNumeric(CurrentPage) Then CurrentPage=int(CurrentPage)
    if CurrentPage<1 Then CurrentPage=1
    if CurrentPage>rs.pagecount  Then
       CurrentPage=rs.pagecount
     End if
     if CurrentPage<=rs.pagecount and rs.pagecount>0 Then
     rs.absolutepage=CurrentPage
     End if
    i = 0
     for j=1 to rs.recordcount
%>
    <tr>
        <td class="td_content"><%=Rs("UserName")%></td>
        <td class="td_content"><%=Rs("RealName")%></a></td>
        <td class="td_content"><%=Rs("Sex")%></td>
        <td class="td_content"><%=Rs("Age")%></td>
        <td class="td_content"><%=Rs("Qualification")%></td>
        <td class="td_content"><%=Rs("JobPost")%></td>
<%
    i = i + 1
    response.Write(i)
    response.Write(j)
    response.Write("<br>")
    Rs.MoveNext
    If i >= Rs.PageSize Then Exit For-------If rs.eof then exit for改为这句为什么又可以了
    next
End if
End If
%>
</table>
<%If Rs.pagecount>1 Then%>
<% If CurrentPage > 1 Then %>
<a href="page.asp?page=<%=CurrentPage-1%>">前一页</a>&nbsp;
  <% Else %>
  首页 上页
  <% End if%>
  <%  If CurrentPage < Rs.pagecount Then %>
<a href="page.asp?page=<%=CurrentPage+1%>">后一页</a>&nbsp;
<% End if%>
<a href="page.asp?page=<%=rs.pagecount%>">末页</a>
<% End if%>
</p>
<p align="center">现在是第<%=CurrentPage%>页一共有<%=rs.pagecount%>页</p>
<%
Rs.Close
%>

</body>
</html>
#7
hams2011-03-31 14:34
If i >= Rs.PageSize Then Exit For
这个是用计数器的,不知你算得对不对

If rs.eof then exit for
这个是判断指针位置的,也就是数据读完了就跳出,结果当然不一样
#8
ab3827170362011-03-31 15:10
计数器的条数难道不等于数据读完时的条数吗
#9
hams2011-03-31 17:09
自己算下
#10
ab3827170362011-03-31 18:37
我算了一下  i和j输出的结果是一样的
#11
hams2011-04-01 08:16
不可能一样
Rs.PageSize =15

假设记录集只有一行数据
循环一次
i=1
这个时候,i >= Rs.PageSize 并不成立,不会跳出
而rs.eof 已经成立了
#12
ab3827170362011-04-02 14:07
<!--#include file="Conn.asp" -->
<!--#include file="Conn1.asp"-->
<!--#include file="Comm/Site_Class.asp" -->
<%
PagePath = ""

Set Qsite = New Site_Class

Qsite.SiteConfig_Master()
Dim Rs,Sql,CurrentPage,FileName,Sql1,Rs1
Dim PersonName,ViewType
Dim PerId
Dim Act,IsHidden,IsLock,IsGj,IsTj,IsYear,IsSh
Dim TodayDate
Dim j,s,m,n,i,x
Dim A(),B(),C()
m=0
n=0
x=0
   Set rs=server.CreateObject("ADODB.Recordset")
   '打开记录集对象
%>
<title>网站公告页面</title>
</head>

<BODY>


<table width="100%" border="0" cellspacing="1" cellpadding="5">
    <tr>
        <td width="8%" class="td_content"><strong>用户名</strong></td>
        <td width="8%" class="td_content"><strong>真实姓名</strong></td>
        <td width="6%" class="td_content"><strong>性别</strong></td>
        <td width="6%" class="td_content"><strong>年龄</strong></td>
        <td width="8%" class="td_content"><strong>学历</strong></td>
        <td width="12%" class="td_content"><strong>求职意向</strong></td>
    </tr>

<%
   Set Rs1=server.CreateObject("ADODB.Recordset")
   Rs1.open "Select * from UserTB",Conn1,1,1
     If Not Rs1.Eof Then
          for j=1 to rs1.recordcount
          ReDim Preserve B(2,rs1.recordcount)
          B(1,j)=Rs1("PerId")
          B(2,j)=Rs1("FUname")
          m=m+1
          rs1.movenext
          next
          End if
     Rs1.close
  Set Rs=server.CreateObject("ADODB.Recordset")
  Rs.open "select * from test",Conn,1,1
  if Not Rs.Eof Then
   for j=1 to Rs.Recordcount
   ReDim Preserve A(2,rs.recordcount)
     A(1,j)=Rs("PerId")
     A(2,j)=Rs("UserName")
     n=n+1
     rs.movenext
     next
     End if
     Rs.close
     s=m+n
     
     
     for i=1 to n
     ReDim preserve B(2,s)
     B(1,m+i)=A(1,i)
     B(2,m+i)=A(2,i)
     next
  Set Rs=server.CreateObject("ADODB.Recordset")
   Rs.open "Select * from PersonTbl order by EditDate desc",Conn,1,1
     If Not Rs.Eof Then
     for j=1 to rs.recordcount
     x=x+1
     ReDim Preserve C(7,rs.recordcount)
     C(2,j)=Rs("sex")
     C(3,j)=Rs("age")
     C(4,j)=Rs("realname")
     C(5,j)=Rs("Qualification")
     C(6,j)=Rs("JobPost")
     C(1,j)=Rs("PerId")
     C(7,j)=""
     rs.movenext
     next
     End if
     Rs.close
     for i=1 to x
       for j=1 to s
        if C(1,i)<>B(1,j) Then
        Else
        C(7,i)=B(2,j)
        End if
        next
     next
  '定义循环变量
 rs.open "select * from PersonTbl order by EditDate desc",Conn,1,1
   rs.pagesize=15
   '得到请求的页号
   CurrentPage=clng(request("page"))
   IF request("page")<>"" then
     CurrentPage=clng(request("page"))
   Else
     CurrentPage=1
   End IF
   IF CurrentPage<1 Then CurrentPage=1
   If CurrentPage>rs.pagecount Then CurrentPage=rs.pagecount
  rs.absolutepage=CurrentPage
  for  i=1 to rs.recordcount--------现在是这样的情况如果用循环 for i=1 to rs.recordcount 时 一个页面就显示完了rs.pagesize根本就没有控制住,
                                    如果用 for i=1 to rs.pagesize 数组C()在rs.pagesize就结束了,我想让数据库记录的数跟数组同时结束,怎么解决
  ReDim Preserve C(7,rs.recordcount)
  %>
  <tr>
        <td class="td_content"><%=C(7,i)%></td>
        <td class="td_content"><%=C(4,i)%>||<%=Rs("realname")%></td>
        <td class="td_content"><%=C(2,i)%>||<%=Rs("sex")%></td>
        <td class="td_content"><%=C(3,i)%>||<%=Rs("age")%></td>
        <td class="td_content"><%=C(5,i)%>||<%=Rs("Qualification")%></td>
        <td class="td_content"><%=C(6,i)%>||<%=Rs("JobPost")%></td>
</tr>
  <%
   rs.MoveNext
   IF rs.Eof Then Exit For
  next
  %>
</table>
<table width="400" border="0">
  <tr>
  <%
  '翻页控制代码
  IF CurrentPage<>1 Then
    Response.Write("<td><a href=aa.asp?page=1>第一页</a></td>")
    Response.Write("<td><a href=aa.asp?page="&(CurrentPage-1)&">上一页</a></td>")
  Else
    Response.Write("<td>第一页</td>")
    Response.Write("<td>上一页</td>")
  End IF   
  IF CurrentPage<>rs.pagecount then
    Response.Write("<td><a href=aa.asp?page=" & (CurrentPage+1) & ">下一页</a></td>")
    Response.Write("<td><a href=aa.asp?page=" & rs.pagecount & ">最后一页</a></td>")
  Else
    Response.Write("<td>下一页</span></td>")
    Response.Write("<td>最后一页</span></td>")
  End IF
  %>
    <td>共<%=rs.pagecount%>页</td>
    <td>当前第<%=CurrentPage%>页</td>
  </tr>
</table>
<%
'网页对象析构代码
rs.Close
conn.Close
Set rs=Nothing
Set conn=Nothing
%>
</body>
</html>

运行后出现重复而且Rs.PageSize=15根本没控制住怎么回事
第一个页面就全部显示完了

路成军     路成军     男     25     本科     程序员
xiaoye2234     王小叶     女     23     大专     助理
richard77     傅振国     男     30     本科     外贸业务员
nancy2002     高琴琼     男     22     本科     翻译
manxin     王温丽     女     22     大专     外贸单证
王秧飞     王秧飞     女     25     高中及以下     金融
denisegong     龚琼盛     女     28     大专     其他
pursuit     梁飞杰     男     22     本科     外贸业务员
yi4218     曹麟义     男     27     大专     外贸跟单
zdy123     周道园     男     26     高中及以下     其它
monicazyj     张郁佳     女     25     大专     外贸采购或助理
第二个页面跟第一个页面重复
王秧飞     王秧飞     女     25     高中及以下     金融
denisegong     龚琼盛     女     28     大专     其他
pursuit     梁飞杰     男     22     本科     外贸业务员
yi4218     曹麟义     男     27     大专     外贸跟单
zdy123     周道园     男     26     高中及以下     其它
monicazyj     张郁佳     女     25     大专     外贸采购或助理


[ 本帖最后由 ab382717036 于 2011-4-2 15:54 编辑 ]
#13
ab3827170362011-04-02 15:55
求助 求助
#14
ab3827170362011-04-02 17:08
哎  自己终于把问题解决了不容易啊
1