如何实现分页浏览?各位高手帮我看看我的源码那里有问题。谢谢(100分)

  • 主题发起人 主题发起人 爱睡的狗狗
  • 开始时间 开始时间

爱睡的狗狗

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:<br> &nbsp; 能不能给我一个完整的可以实现分页浏览的示例程序,就像有“显示全部信息,,分页显示,上一页,下一页”。<br> &nbsp;我现在可以实现部分功能,可是上一页,下一页总是有bug,例如,第一次单击“下一页”时,显示的是单击“分页浏览”显示的内容(注一次显示5条记录),只有第二次单击“下一页”时才会增加。<br>这是源码,帮我看看那里有问题:<br><br>procedure TForm1.Button1Click(Sender: TObject); //显示全部信息<br> &nbsp;begin<br> &nbsp; &nbsp;with Query1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp;SQL.Clear;<br> &nbsp; &nbsp; &nbsp;SQL.Add('select * from blacktable ORDER BY [NO]');<br> &nbsp; &nbsp; &nbsp;open;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br>procedure TForm1.Button2Click(Sender: TObject); &nbsp;//分页显示信息<br> &nbsp; begin<br> &nbsp; &nbsp; with Query1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp;SQL.Clear;<br> &nbsp; &nbsp; &nbsp;sql.add('select top 5 * from blacktable where NO not in (select top 0 NO from blacktable)');<br> &nbsp; &nbsp; &nbsp;open;<br> &nbsp; end;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject); &nbsp;//上一页<br> &nbsp;begin<br> &nbsp; &nbsp;with Query1 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp; &nbsp;SQL.Clear;<br> &nbsp; &nbsp; &nbsp; &nbsp;per_page_count:=5;<br> &nbsp; &nbsp; &nbsp; &nbsp;sql.add('select top '+inttostr(per_page_count)+' * from blacktable where NO not in (select top '+inttostr(PrevPageVariant)+' NO from blacktable)');<br> &nbsp; &nbsp; &nbsp; &nbsp;dec(PrevPageVariant,5);<br> &nbsp; &nbsp; &nbsp; &nbsp;open;<br> &nbsp; &nbsp; end;<br> &nbsp;end;<br><br>procedure TForm1.Button4Click(Sender: TObject); &nbsp;//下一页<br> &nbsp;begin with Query1 do<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; Close;<br> &nbsp; &nbsp; &nbsp; SQL.Clear;<br> &nbsp; &nbsp; &nbsp; per_page_count:=5;<br> &nbsp; &nbsp; &nbsp;sql.add('select top '+inttostr(per_page_count)+' * from blacktable where NO not in (select top '+inttostr(PrevPageVariant)+' NO from blacktable)');<br> &nbsp; &nbsp; &nbsp;inc(PrevPageVariant,5);<br> &nbsp; &nbsp; &nbsp;open;<br> &nbsp; &nbsp;end;<br> &nbsp;end;
 
procedure TFrmYW_BGCX.FindByPage(PageIndex: integer);<br>begin<br> &nbsp; with DM.QBG do<br> &nbsp; begin<br> &nbsp; &nbsp;Screen.Cursor:=crHourGlass;<br> &nbsp; &nbsp;DisableControls;<br> &nbsp; &nbsp;Tick:=GetTickCount;<br> &nbsp; &nbsp;DM.GetBGPageID(PageIndex);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;close;<br> &nbsp; &nbsp;sql.Clear;<br> &nbsp; &nbsp;sql.Add('SELECT top 100 * FROM '+ViewName);<br> &nbsp; &nbsp;SQL.Add('WHERE (1=1)');<br> &nbsp; &nbsp;sql.Add(WHERESQL);<br> &nbsp; &nbsp;if PageID&lt;&gt;'' then<br> &nbsp; &nbsp; &nbsp; sql.Add(' AND (报告编号&lt;'''+PageID+''')');<br> &nbsp; &nbsp;sql.Add('ORDER BY 报告编号 DESC');<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;Open;<br> &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp;EnableControls;<br> &nbsp; &nbsp; &nbsp;Screen.Cursor:=crDefault;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;EnableControls;<br> &nbsp; &nbsp;Screen.Cursor:=crDefault;<br> &nbsp; &nbsp;lbl_Num.Caption:=' &nbsp;共有数据:'+IntToStr(GNum)+' :(总共 '+IntToStr(PageNum)+' 页,每页100条)';<br> &nbsp; &nbsp;lbl_Num.Caption:=lbl_Num.Caption+' ----执行查询时间:'+FloatToStr((GetTickCount-Tick)/1000)+' 秒';<br> &nbsp; end;<br>end;<br><br>procedure TFrmYW_BGCX.bFirstClick(Sender: TObject);<br>begin<br> &nbsp; &nbsp; &nbsp;cbPage.ItemIndex:=0;<br> &nbsp; &nbsp; &nbsp;FindByPage(cbPage.ItemIndex);<br>end;<br><br>procedure TFrmYW_BGCX.bPriorClick(Sender: TObject);<br>begin<br> &nbsp;if cbPage.ItemIndex&gt;0 then<br> &nbsp; &nbsp; &nbsp;cbPage.ItemIndex:=cbPage.ItemIndex-1;<br> &nbsp; &nbsp; &nbsp;FindByPage(cbPage.ItemIndex);<br>end;<br><br>procedure TFrmYW_BGCX.bNextClick(Sender: TObject);<br>begin<br> &nbsp;if cbPage.ItemIndex&lt;cbPage.Items.Count-1 then<br> &nbsp; &nbsp; &nbsp;FindByPage(cbPage.ItemIndex+1);<br> &nbsp; &nbsp; &nbsp;cbPage.ItemIndex:=cbPage.ItemIndex+1;<br>end;<br><br>procedure TFrmYW_BGCX.bLastClick(Sender: TObject);<br>begin<br> &nbsp; &nbsp; cbPage.ItemIndex:=cbPage.Items.Count-1;<br> &nbsp; &nbsp; &nbsp;FindByPage(cbPage.ItemIndex);<br>end;<br><br>procedure TFrmYW_BGCX.cbPageChange(Sender: TObject);<br>begin<br> &nbsp; FindByPage(cbPage.ItemIndex);<br>end;<br>procedure TDM.GetBGPageID(APageIndex: integer);<br>begin<br> &nbsp;PageID:='';<br> &nbsp;if APageIndex&gt;0 then<br> &nbsp;with Q_Para do<br> &nbsp;begin<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;close;<br> &nbsp; &nbsp;sql.Clear;<br> &nbsp; &nbsp;sql.Add('SELECT 报告编号 FROM '+ViewName);<br> &nbsp; &nbsp;sql.Add('WHERE (1=1)');<br> &nbsp; &nbsp;SQL.Add(WHERESQL);<br> &nbsp; &nbsp;sql.Add('ORDER BY 报告编号 DESC');<br> &nbsp; &nbsp;Open;<br> &nbsp; &nbsp;First;<br> &nbsp; &nbsp;if not IsEmpty then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;MoveBy(APageIndex*200+1);<br> &nbsp; &nbsp;PageID:=Trim(FieldByName('报告编号').AsString);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Close;<br> &nbsp;end;<br>end;<br>不过最好可以考虑服务器游标的分页..<br>如果数据量大的话就要必须的分页,读入数据的时候多慢啊!<br>可以用存储过程来分页的啊!我给你个例子你看看!<br>CREATE PROC ReturnRowspub(<br> @SQL VARCHAR(100),--表或试图的名称<br> @Page int,--当前的页数<br> @findstr &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varchar(255),---需要查询的值<br> @ID VARCHAR(255),--主键<br> @Sort VARCHAR(255),--需要排序的字段<br> @findzd varchar(255)---需要查询的字段)<br>AS<br>DECLARE @RecsPerPage int,--每页显示的记录数量(当然你可以设置成动态的)<br> &nbsp; &nbsp; &nbsp; &nbsp;@sqlstr varchar(1000)--需要执行的SQL语句<br> set @RecsPerPage=30<br>if @findstr='' or &nbsp;@findzd='' ---表示普通的浏览<br> SET @sqlStr='SELECT &nbsp; TOP &nbsp;'+CAST(@RecsPerPage AS VARCHAR(20))+' * &nbsp;FROM '+@SQL+' &nbsp;T WHERE T.'+@ID+' not in <br> (SELECT TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM '+@SQL+' T ORDER BY '+@Sort+') ORDER BY '+@Sort<br>else &nbsp; &nbsp;---查询浏览<br> &nbsp; SET @sqlStr='SELECT &nbsp; TOP &nbsp;'+CAST(@RecsPerPage AS VARCHAR(20))+' * &nbsp;FROM '+@SQL+' &nbsp;T WHERE T.'+@ID+' not in <br> (SELECT TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM '+@SQL+' T where t.'+@findzd+' like '''+@findstr+'%'' ORDER BY '+@Sort+') <br> and t.'+@findzd+' like '''+@findstr+'%'' ORDER BY '+@Sort<br>exec (@strSQL) <br>go<br>这个存储过程我一直在用,至于效率(40万条数据量)还不错,挺快!如果还有什么问题
 
我先试试,如果有问题再问,还请高手在这段时间多多关注和多多帮忙!!!!![:D]<br>谢谢!!!!!!!
 
蓝叶菱你好,能不能将源码加注释,有点晕,还有实现的好像是带索引的分页显示,我想要的是按键式的分页显示,能不能帮我想想。谢谢
 
procedure TForm1.Button3Click(Sender: TObject); &nbsp;//上一页<br> &nbsp;begin<br> &nbsp; &nbsp;with Query1 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Close;<br> &nbsp; &nbsp; &nbsp; &nbsp;SQL.Clear;<br> &nbsp; &nbsp; &nbsp; &nbsp;per_page_count:=5;<br> &nbsp; &nbsp; &nbsp; &nbsp;dec(PrevPageVariant,5);<br> &nbsp; &nbsp; &nbsp; &nbsp;sql.add('select top '+inttostr(per_page_count)+' * from blacktable where NO not in (select top '+inttostr(PrevPageVariant)+' NO from blacktable)');<br> &nbsp; &nbsp; &nbsp; &nbsp;//dec(PrevPageVariant,5);<br> &nbsp; &nbsp; &nbsp; &nbsp;open;<br> &nbsp; &nbsp; end;<br> &nbsp;end;<br><br>procedure TForm1.Button4Click(Sender: TObject); &nbsp;//下一页<br> &nbsp;begin with Query1 do<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; Close;<br> &nbsp; &nbsp; &nbsp; SQL.Clear;<br> &nbsp; &nbsp; &nbsp; inc(PrevPageVariant,5);<br> &nbsp; &nbsp; &nbsp; per_page_count:=5;<br> &nbsp; &nbsp; &nbsp;sql.add('select top '+inttostr(per_page_count)+' * from blacktable where NO not in (select top '+inttostr(PrevPageVariant)+' NO from blacktable)');<br> &nbsp; &nbsp; &nbsp;//inc(PrevPageVariant,5);<br> &nbsp; &nbsp; &nbsp;open;<br> &nbsp; &nbsp;end;<br> &nbsp;end; &nbsp;<br><br>看到我的改动了吧
 
我需要处理的是由一个库的多个表通过select语句选出的数据集,不是针对的某一具体的表,请问各位有没有高招,有人说DBGride有分页的功能,可是我不会,还请各位高手多多帮助!!!!!
 
这样通过SQL分页是不对的。<br>因为你是程序不是网页。<br>还是建议你用DbExpress吧。<br><br>技术交流QQ:136293586
 
muhx兄,我试了你的代码,很好,这次运行正确,可是多次点击“上一页”或“下一页”后在单击“分页显示”后,虽然可以显示前5条,可是在单击“上一页”或“下一页”你会发现它还会从当初的记录操作,而不是在一次从新显示,如何当单击“分页显示”后,清空所有记录,使之从头开始!还有当单击“上一页”到1-5条后,再单击“上一页”会出现错误,如何改?还有就是刚才说的我想处理的对象是一个数据集,而不是一个具体的表,有没有高招?能不能给我你的qq,这样问有点麻烦?或e-mail?
 
蓝叶菱兄:DM.QBG是什么?是控件吗?这几个过程是自己定义的吗?还有在delphi中如何使用存储过程?我是新手,还请多多帮助。
 
select top 50 * from youTable &nbsp;where &nbsp;youid &gt; &nbsp;'当前' //下一页<br>select top 50 * from youTable &nbsp;where &nbsp;youid &lt; &nbsp;'当前' ORDER BY youid DESC//上一页
 
网中戏兄,我想知道的是如何对显示在DBGride 中的查询结果进行分页显示,而不是对特定的一个表,还请指点!
 
if exists (select * from sysobjects where id = object_id('[dbo].[SP_Page]') and objectproperty(id, 'isprocedure') = 1)<br>drop procedure [dbo].[SP_Page]<br>go<br><br>create procedure SP_Page &nbsp; <br>@sql nvarchar(4000), --要执行的sql语句<br>@currentpage int=2, &nbsp;--要显示的页码<br>@pagesize int=10, &nbsp; &nbsp;--每页的大小<br>@pagecount int=0 out --总页数<br>with encryption<br>as<br>set nocount on<br>declare @p1 int<br><br>exec sp_cursoropen @p1 output,@sql,@scrollopt=1,@ccopt=1,@rowcount=@pagecount output<br><br>select @pagecount=ceiling(1.0*@pagecount/@pagesize),<br> &nbsp; &nbsp; &nbsp; @currentpage=(@currentpage-1)*@pagesize+1<br>--select @currentpage<br>exec sp_cursorfetch @p1,16,@currentpage,@pagesize <br>exec sp_cursorclose @p1<br><br>go<br>declare @pc int<br>exec sp_page 'select * from a',1,20,@pc output<br>--select * from sob
 
现在我明白一点了,要想实现查询结果的分页显示需要视图,由存储过程对视图进行处理,有谁知道相关的内容?
 
如何将查询条件形成一个视图,并由一个存储过程对它进行操作,后由DBGride显示查询结果。
 
To 楼主:<br> &nbsp;我在 delphi 盒子里贴了一个程序,可以满足你的要求,可以参考一下。<br> &nbsp;http://www.2ccc.com/article.asp?articleid=3520<br> &nbsp;这个程序不像您想象的那样是针对“一个具体的表”,它是通用的,只要您改变 ADODataSet的 CommandText 的内容就行了。另外可以告诉您 Delphi 的数据感知控件不支持分页,用它的话你只能在SQL 上下工夫...
 
To vvyang 你好,我现在用的是ODBC连的数据库,这样的话,你的例子还适用吗?如果不行还有没有别的方法,特急!!!!!!!!!!!!!!!!!!!!
 
在线等!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
To 爱睡的狗狗:<br> &nbsp;貌似 ADOConnection 也可以连接 ODBC...
 
To vvyang 照您的意思ADOConnection 也可以连接 ODBC的,并且与具体的后台数据库无关是吗?我是新手,有很多东西不懂,你在delphi盒子的帖子我已经看了,并加你的qq为好友,你看看就知道了头像是个海豚,号是610756763,如果不介意的话我想和你聊聊,因为你的东西有一些我看不懂,现在有需要实现,所以有很多想问你。还有你的例子中的页数是已知的,可是查询的结果的数量是未知的。改怎样处理,还请多多指教。长期在线!!!
 
CREATE PROC ReturnRowspub(<br> &nbsp; @SQL &nbsp; &nbsp;VARCHAR(100),--表或试图的名称<br> &nbsp; @Page &nbsp; &nbsp;int,--当前的页数<br> @findstr &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varchar(255),---需要查询的值<br> @ID &nbsp; &nbsp;VARCHAR(255),--主键<br> &nbsp; @Sort &nbsp; &nbsp;VARCHAR(255),--需要排序的字段<br> @findzd &nbsp; &nbsp;varchar(255)---需要查询的字段)<br>AS<br>DECLARE @RecsPerPage &nbsp;int,--每页显示的记录数量(当然你可以设置成动态的)<br> &nbsp; &nbsp; &nbsp; &nbsp;@sqlstr varchar(1000)--需要执行的SQL语句<br> set @RecsPerPage=30<br>if @findstr='' or &nbsp;@findzd='' ---表示普通的浏览<br> SET @sqlStr='SELECT &nbsp; TOP &nbsp;'+CAST(@RecsPerPage AS VARCHAR(20))+' * &nbsp;FROM '+@SQL+' &nbsp;T WHERE T.'+@ID+' not in <br> (SELECT TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM '+@SQL+' T ORDER BY '+@Sort+') ORDER BY '+@Sort<br>else &nbsp; &nbsp;---查询浏览<br> &nbsp; SET @sqlStr='SELECT &nbsp; TOP &nbsp;'+CAST(@RecsPerPage AS VARCHAR(20))+' * &nbsp;FROM '+@SQL+' &nbsp;T WHERE T.'+@ID+' not in <br> (SELECT TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM '+@SQL+' T where t.'+@findzd+' like '''+@findstr+'%'' ORDER BY '+@Sort+') <br> &nbsp;and t.'+@findzd+' like '''+@findstr+'%'' ORDER BY '+@Sort<br>exec (@strSQL) <br>go
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
618
import
I
后退
顶部