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

  • 主题发起人 主题发起人 爱睡的狗狗
  • 开始时间 开始时间
DM.QBG是什么?就是ADOquery...<br>这个是从我代码中截取的,没有加工,你看懂就可以了..<br>这几个过程是自己定义的吗?<br>还有在delphi中如何使用存储过程?我是新手,还请多多帮助。
 
呵呵..蓝大侠的分页储存过程还蛮好用的..不过,不知是不小心还是...[:D][:D]...<br>倒数第二句 &nbsp; exec (@strSQL) &nbsp;应为exec (@sqlStr) &nbsp;,<br>另蓝大侠储存过程的简单用法:望蓝大侠莫见笑::<br>exec ReturnRowspub &nbsp;'pubs..employee','2',' ','emp_id','emp_id','fname'<br>go<br>建议LZ参考一下有关储存过程的资料..
 
使用原生ADO对数据进行分页显示<br>下面的页面中有Demo下载:<br>http://www.2ccc.com/article.asp?articleid=3520<br>源码如下:<br>unit MainFrm;<br><br>interface<br><br>uses<br> &nbsp;Classes, Controls, Forms, Grids, ToolWin, ComCtrls, StdCtrls, ExtCtrls, DB,<br> &nbsp;ADODB;<br><br>type<br> &nbsp;TfrmMain = class(TForm)<br> &nbsp; &nbsp;cnn: TADOConnection;<br> &nbsp; &nbsp;rst: TADODataSet;<br> &nbsp; &nbsp;pnlNavigate: TPanel;<br> &nbsp; &nbsp;btnFirst: TButton;<br> &nbsp; &nbsp;btnPrior: TButton;<br> &nbsp; &nbsp;btnNext: TButton;<br> &nbsp; &nbsp;btnLast: TButton;<br> &nbsp; &nbsp;edtPageNO: TEdit;<br> &nbsp; &nbsp;sgData: TStringGrid;<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure btnFirstClick(Sender: TObject);<br> &nbsp; &nbsp;procedure edtPageNOKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);<br> &nbsp;private<br> &nbsp; &nbsp;FPageNo: Integer;<br> &nbsp; &nbsp;procedure SetPageNo(Value: Integer);<br> &nbsp;public<br> &nbsp; &nbsp;property PageNo: Integer read FPageNo write SetPageNo;<br> &nbsp;end;<br><br>var<br> &nbsp;frmMain: TfrmMain;<br><br>implementation<br><br>uses<br> &nbsp;Windows, SysUtils;<br><br>{$R *.dfm}<br><br>const<br> &nbsp;PAGE_SIZE = 6;<br><br>procedure TfrmMain.FormCreate(Sender: TObject);<br>var<br> &nbsp;i: Integer;<br>begin<br> &nbsp;SetWindowLong(edtPageNO.Handle, GWL_STYLE, GetWindowLong(edtPageNo.Handle,<br> &nbsp; &nbsp;GWL_STYLE) or ES_CENTER or ES_NUMBER);<br> &nbsp;rst.Active := True;<br> &nbsp;rst.Recordset.PageSize := PAGE_SIZE;<br> &nbsp;sgData.ColCount := rst.FieldCount + 1;<br> &nbsp;for i := 0 to rst.FieldCount - 1 do<br> &nbsp;begin<br> &nbsp; &nbsp;sgData.ColWidths[i + 1] := rst.Fields.DisplayWidth * Canvas.TextWidth('0');<br> &nbsp; &nbsp;sgData.Cells[i + 1, 0] := rst.Fields.DisplayName;<br> &nbsp;end;<br> &nbsp;sgData.Cells[0, 0] := '序号';<br> &nbsp;PageNo := 1;<br>end;<br><br>procedure TfrmMain.btnFirstClick(Sender: TObject);<br>begin<br> &nbsp;if Sender = btnFirst then<br> &nbsp; &nbsp;PageNo := 1<br> &nbsp;else if Sender = btnPrior then<br> &nbsp; &nbsp;PageNo := PageNo - 1<br> &nbsp;else if Sender = btnNext then<br> &nbsp; &nbsp;PageNo := PageNo + 1<br> &nbsp;else PageNo := rst.Recordset.PageCount;<br>end;<br><br>procedure TfrmMain.edtPageNOKeyDown(Sender: TObject; var Key: Word;<br> &nbsp;Shift: TShiftState);<br>begin<br> &nbsp;if edtPageNO.Text &lt;&gt; '' then<br> &nbsp; &nbsp;PageNo := StrToInt(edtPageNO.Text);<br>end;<br><br>procedure TfrmMain.SetPageNo(Value: Integer);<br>const<br> &nbsp;adStateOpen = $00000001;<br>var<br> &nbsp;n, i, j: Integer;<br>begin<br> &nbsp;with rst.Recordset do<br> &nbsp; &nbsp;if (FPageNo &lt;&gt; Value) and (Value &gt;= 1) and (Value &lt;= PageCount) and<br> &nbsp; &nbsp; &nbsp;(State = adStateOpen) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FPageNo := Value;<br> &nbsp; &nbsp; &nbsp;AbsolutePage := FPageNo;<br> &nbsp; &nbsp; &nbsp;n := (FPageNo - 1) * PAGE_SIZE;<br> &nbsp; &nbsp; &nbsp;edtPageNo.Text := IntToStr(FPageNo);<br> &nbsp; &nbsp; &nbsp;for i := 1 to PAGE_SIZE do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;sgData.RowCount := i + 1;<br> &nbsp; &nbsp; &nbsp; &nbsp;sgData.Cells[0, i] := IntToStr(n + i);<br> &nbsp; &nbsp; &nbsp; &nbsp;for j := 0 to Fields.Count - 1 do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sgData.Cells[j + 1, i] := Fields[j].Value;<br> &nbsp; &nbsp; &nbsp; &nbsp;MoveNext;<br> &nbsp; &nbsp; &nbsp; &nbsp;if Eof then Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br>end;<br><br>end.
 
To mydecpu:谢谢,提醒。现在正在恶补有关存储过程的知识,努力ing!!!
 
如何在query.open前SQL.Add(seclect语句)后对结果集进行控制,让它可以实现分页显示,有没有方法建立一个临时表之类的,而后对这个表进行处理。
 
CREATE PROCEDURE Pr_GetDataPage <br>@CurrentPage int,<br>@PageSize int<br>AS<br>Declare @ID int<br>Declare @MoveRecords int<br><br>--@CurrentPage和@PageSize是传入参数<br>Set @MoveRecords=@CurrentPage * @PageSize+1<br><br>--下面两行实现快速滚动到我们要取的数据的行,并把ID记录下来<br>Set Rowcount @MoveRecords<br>Select @ID=pid from products Order by pid<br><br>Set Rowcount @PageSize<br>--最恨为了减少麻烦使用*了,但是在这里为了说明方便,暂时用一下<br>Select * From products Where pid&gt;=@ID Order By pid<br>Set Rowcount 0<br><br>GO
 
各位高手,有没有不用存储过程的方法来实现分页呢,我的东西要求和后台的数据库是无关的。如果用存储过程的话,后台的限制就会大了很多,这也是我发愁的原因。还有用DBgride进行分页显示,看了很多都是dbstring的,可是如果改成dbstring的我的导出功能还要重写,更麻烦。
 
To 爱睡的狗狗:<br> &nbsp;我只想问楼主一句,您到底准备用什么方法,一会存储过程,一会ADO,一会临时表,真是服了你了!!!!<br> &nbsp;看来,您缺的不仅仅是技术...
 
To vvyang: 不是我不专,只是不懂的太多,不知该怎么做好。所以才会有这样的结果。请不要介意
 
吐血奉献,最新瑞星2006版注册机,好的顶!<br>http://web.first01.net//aaa/rav.rar
 
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);//viewName就是你要查询的表格,取100条,按照条件取得。<br> &nbsp; &nbsp;SQL.Add('WHERE (1=1)');<br> &nbsp; &nbsp;sql.Add(WHERESQL);<br> &nbsp; &nbsp;if PageID&lt;&gt;'' then//如果PAGEID是不是空,按照报告编号进行筛选。。<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;//转到第一个一页<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; 转到下一页<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; 转到下一页。<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; 最后一页。<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;转到任意页。<br> &nbsp; FindByPage(cbPage.ItemIndex);<br>end;<br>procedure TDM.GetBGPageID(APageIndex: integer);<br>begin<br> &nbsp;//公用函数,主要是取得究竟有多少也,其中传递了条件。。<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> var<br> &nbsp;total_count: integer;<br> &nbsp;a:string;<br> &nbsp;begin<br> &nbsp;a:=trim(IPEdit1.IPString);<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 count(*) from blacktable b,filefix f where b.NO not in (select top '+inttostr(PrevPageVariant)+' F.NO from filefix k ,blacktable f where k.ip='''+a+''' and k.ip=f.ip and f.name=k.type order by k.no ) and b.name=f.type and b.ip=f.ip and b.ip='''+a+'''');<br> &nbsp; &nbsp; &nbsp; open;<br> &nbsp; &nbsp; &nbsp; per_page_count:=5;<br> &nbsp; &nbsp; &nbsp; Total_Count := Query1.Fields[0].Value;<br> &nbsp; &nbsp; &nbsp; PrevPageVariant:=((Total_Count-1) div 5)+1;<br> &nbsp; &nbsp; &nbsp; sql.add('select top '+inttostr(per_page_count)+' b.no, b.Name,b.title,b.ip,f.fix,f.descr from blacktable b,filefix f where b.NO not in (select top '+inttostr(PrevPageVariant)+' F.NO from filefix k ,blacktable f where k.ip='''+a+''' and k.ip=f.ip and f.name=k.type order by k.no ) and b.name=f.type and b.ip=f.ip and b.ip='''+a+''' &nbsp;order by b.no');<br> &nbsp; &nbsp; &nbsp; open;<br> &nbsp; &nbsp; &nbsp; DBGrid1.DataSource:=DataSource1;<br> &nbsp; &nbsp; &nbsp; end;
 
多人接受答案了。
 

Similar threads

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