如何实现在记录的前后移动??70分高分求教!

iStringTheory 2001-09-08 11:29:32
我想做一个这样的效果,当查看一条资料的时候,可以通过“上一条”、“下一条”等连接分别查看上一条与下一条记录,记录在数据库中的索引是个“自动编号”,由于删除记录操作导致索引号不连续,显然不能通过当前索引号减一的方法实现记录的前后移动,那么该如何实现呢??谢谢!
...全文
156 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Antsoldier 2001-09-11
  • 打赏
  • 举报
回复
为什么不给我分?
wybm 2001-09-10
  • 打赏
  • 举报
回复
<%@ Language=VBScript %>
<%
set conn=server.CreateObject ("adodb.connection")
conn.Open "dsn=web;uid=sa;pwd="
'使用pubs数据库
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="PreRs.asp">
<div align="center">
<table width="100%" border="0">
<tr>
<td width="26%">
<div align="right">Authors : </div>
</td>
<td width="74%">
<select name="List">
<%
sql="select DISTINCT au_lname from authors"
set rs_list=conn.Execute(sql)
if rs_list.eof then
Response.Write "<option value=''>No Author</option>"
else
do while not rs_list.eof
Response.Write "<option value=" & rs_list("au_lname") & ">" & rs_list("au_lname") & "</option>"
rs_list.movenext
loop
end if
rs_list.close
set rs_list=nothing
%>
</select>
</td>
</tr>
</table>

<input type="submit" name="Submit" value="search">
<input type="hidden" name="action">
</div>
</form>
<%
au_lname=Request.Form ("list")
au_lname=replace(au_lname,"'","''")
action=Request("action")
if action="movenext" then
'得到后一条记录
record=Request("record")
record=replace(record,"'","''")
set OUTRS=server.CreateObject ("adodb.recordset")
STRSQL="select * from authors order by au_lname"
OUTRS.Open STRSQL,conn,1,1
OUTRS.Find "au_lname='" & record & "'"
if OUTRS.eof then
Response.Write "No Record"
Response.End
else
OUTRS.MoveNext
OUTRS.movenext

if OUTRS.EOF then
Response.Write "No"
no_next=true
else
Response.Write "yes"
no_next=false
end if
OUTRS.moveprevious
end if
elseif action="moveprev" then
'得到前一条记录
record=Request("record")
record=replace(record,"'","''")
set OUTRS=server.CreateObject ("adodb.recordset")
STRSQL="select * from authors order by au_lname"
OUTRS.Open STRSQL,conn,1,1
OUTRS.Find "au_lname='" & record & "'"
if OUTRS.eof then
Response.Write "No Record"
Response.End
else
OUTRS.Moveprevious
OUTRS.Moveprevious
if OUTRS.BOF then
no_prev=true
else
no_prev=false
end if
OUTRS.movenext
end if
else
'得到当前记录
record=Request("List")
set OUTRS=server.CreateObject ("adodb.recordset")
STRSQL="select * from authors order by au_lname"
OUTRS.Open STRSQL,conn,1,1
OUTRS.Find "au_lname='" & au_lname & "'"

if OUTRS.EOF then
OUTRS.MoveFirst
no_prev=true
OUTRS.movenext
if OUTRS.eof then
no_next=true
end if
OUTRS.moveprevious
else
OUTRS.movenext
if OUTRS.eof then
no_next=true
end if
OUTRS.moveprevious
OUTRS.moveprevious
if OUTRS.bof then
no_prev=true
end if
OUTRS.movenext
end if
end if
%>
<table width="100%" border="1">
<tr>
<%
for each title in OUTRS.fields
%>
<td width="26%"><%=title.name%></td>
<%
next
%>
</tr>
<tr>
<%
if not(OUTRS.eof) then
for each FieldsValue in OUTRS.fields
%>
<td>
<%=FieldsValue.value%>
</td>
<%
next
else
Response.Write "<td>No Record</td>"
end if
%>
</tr>
</table>

<%if no_prev=false then%>
<a href="PreRS.asp?action=moveprev&record=<%=OUTRS("au_lname")%>"><<</a>
<%
else
%>
<<
<%end if%>
<%if no_next=false then%>
<a href="PreRS.asp?action=movenext&record=<%=OUTRS("au_lname")%>">>></a>
<%
else
%>
>>
<%end if
%>
</body>
</html>
www525 2001-09-10
  • 打赏
  • 举报
回复
Antsoldier(兵蚁)的回答的非常好
Antsoldier 2001-09-09
  • 打赏
  • 举报
回复

让数据库中的记录用上一条下一条显示


若要让RecordSet移动到上一条下一条的位置,让我们先学会以下RecordSet对象的属性和方法:

BOF属性:当前游标指到RecordSet的第一条记录。
EOF属性:当前游标指到RecordSet的最后一条记录。
Move方法:移动游标到RecordSet中的某一个记录。
AbsolutePosition属性:当前游标在RecordSet中的位置。
bookmark(书签)属性:对RecordSet的一条记录做一个记号。

详细介绍如下:

BOF与EOF属性

可以编写程式码来检查BOF与EOF属性,来得知当前游标RecordSet的位置:

BOF与EOF都为False:表示游标位于RecordSet当中。
BOF为True:当前游标指到RecordSet的第一条记录。
EOF为True:当前游标指到RecordSet的最后一条记录。
BOF与EOF都为True:在RecordSet里没有任何的资料记录。
Move方法

您可以用Move方法移动游标到RecordSet中的某一条记录:

MoveFirst方法:移至第一条记录。
MoveLast方法:移至最后一条记录。
MoveNext方法:移至下一条记录。
MovePrevious方法:移至上一条记录。
Move [n]方法:移动游标到第n条记录,n由0算起。
AbsolutePosition属性

若您需要确定当前游标在RecordSet中的位置,您可以用AbsolutePosition属性。

AbsolutePosition属性的数值为当前游标相对于第一条的位置,由1算起,即第一条的AbsolutePosition为1。

然而,不要误以为AbsolutePosition是资料记录的编号,如果当前RecordSet处于未定的状态,则

AsolutePosition的数值为1。

另外,在存取RecordSet时,无法保证RecordSet每次都以同样的顺序出现。

若要启用AbsolutePosition,必须先设定为使用用户端cursor,asp码如下:

rs2.CursorLocation = 3


一、Move/AbsolutePosition的例子

让我们看一个於ASP程式码当中使用Move/AbsolutePosition做到上一条下一条记录功能的例子。

譬如ASP程式码rs14.asp如下:

<%

Set conn1 = Server.CreateObject("ADODB.Connection")

conn1.Open "DBQ="& Server.MapPath("ntopsamp.mdb") &";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS Access;"

Set rs2 = Server.CreateObject("ADODB.Recordset")

SqlStr = "SELECT * From 着作"

'Enable AbsolutePosition

rs2.CursorLocation = 3 'adUseClient

rs2.Open SqlStr,conn1,1,1

if Request("sel") = "prev" then

Session("position") = Session("position") - 1

rs2.Move Session("position")

elseif Request("sel") = "next" then

Session("position") = Session("position") + 1

rs2.Move Session("position")

else

Session("position") = 0

end if

%>

<% = Session("position") %>

<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>

<TR>

<TD ALIGN=CENTER BGCOLOR="#008080"><FONT COLOR="#FFFFFF">书名</FONT></TD>

<TD ALIGN=CENTER BGCOLOR="#008080"><FONT COLOR="#FFFFFF">出版</FONT></TD>

<TD ALIGN=CENTER BGCOLOR="#008080"><FONT COLOR="#FFFFFF">图片</FONT></TD>

<TD ALIGN=CENTER BGCOLOR="#008080"><FONT COLOR="#FFFFFF">简介</FONT></TD>

<TD ALIGN=CENTER BGCOLOR="#008080"><FONT COLOR="#FFFFFF">AbsolutePosition</FONT></TD>

<TR>

<TD BGCOLOR="f7efde" ALIGN=CENTER><%= rs2("书名") %></TD>

<TD BGCOLOR="f7efde" ALIGN=CENTER><%= rs2("出版") %></TD>

<TD BGCOLOR="f7efde" ALIGN=CENTER><%= rs2("图片") %></TD>

<TD BGCOLOR="f7efde" ALIGN=CENTER><%= rs2("简介") %></TD>

<TD BGCOLOR="f7efde" ALIGN=CENTER><%= rs2.AbsolutePosition %></TD>

</TR>

</TABLE>

<% if Session("position") > 0 then %>

<A href=rs14.asp?sel=prev>上一条</A>

<% end if %>

<% if Session("position") < rs2.RecordCount -1 then %>

<A href=rs14.asp?sel=next>下一条</A>

<% end if %>

<% rs2.Close %>

以上的 ASP程式码rs14.asp,在用户端使用浏览器,浏览执行的结果,显示上一条下一条的功能。

由Session("position")储存这个使用者的RecordSet游标位置,当按下 [上一条] 超级连接时,由<A href=rs14.asp?sel=prev>上一条</A>,将sel设定为prev,并重新执行rs14.asp的asp码。由以下的程式部份,

将Session("position")减一:

if Request("sel") = "prev" then

Session("position") = Session("position") - 1

rs2.Move Session("position")

elseif Request("sel") = "next" then

Session("position") = Session("position") + 1

rs2.Move Session("position")

else

Session("position") = 0

end if

然后使用Move Session("position")方法,将移动游标到第Session("position")个记录,第一条记录的Session("position")值为0。

当按下 [下一条] 超级连接时,由<A href=rs14.asp?sel=next>下一条</A>,将sel设定为next,并重新执行rs14.asp的asp码。由以上的程式部份,将Session("position")加一,然后使用Move Session("position")方法,将移动指标到第Session("position")个记录。

AbsolutePosition部份,首先由rs2.CursorLocation = 3设定为使用用户端cursor,以启用AbsolutePosition,即可由<%= rs2.AbsolutePosition %>显示AbsolutePosition的值。

为了判断是否要显示 [上一条] [下一条] 超级连接,由以下的程式部份:

<% if Session("position") > 0 then %>

<A href=rs14.asp?sel=prev>上一条</A>

<% end if %>

<% if Session("position") < rs2.RecordCount -1 then %>

<A href=rs14.asp?sel=next>下一条</A>

<% end if %>

就可以使用if Session("position") > 0、if Session("position") < rs2.RecordCount -1,比较当前的位置Session("position")和第一条0、第末条RecordCount -1的位置,来判断是否要显示 [上一条] [下一条] 的超级连接。
sdly 2001-09-08
  • 打赏
  • 举报
回复
应全部打开,在分别显示!
iStringTheory 2001-09-08
  • 打赏
  • 举报
回复
to:i_need_mana(小岭) 
我先打开了一条记录
select * from table where id=9
再怎样移动记录?
iStringTheory 2001-09-08
  • 打赏
  • 举报
回复
to:wanglei0802(wanglei) 
是的,改变在数据库中的位置
i_need_mana 2001-09-08
  • 打赏
  • 举报
回复
打开时用order by 如:Select * from table1 order by fld1
然后就可以用rs1.movelast或rs1.movenext了!
wanglei0802 2001-09-08
  • 打赏
  • 举报
回复
改变在数据库中的位置?
iStringTheory 2001-09-08
  • 打赏
  • 举报
回复
在recordset中移动
sdly 2001-09-08
  • 打赏
  • 举报
回复
记录集移动?

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧