SQL语句分页

2016-08-22 10:16:31来源:http://onepc.blog.51cto.com/9443/148339作者:onepc人点击


unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids, Buttons, ComCtrls, ExtCtrls;typeTForm1 = class(TForm)ADOConn: TADOConnection;ADOQ: TADOQuery;DBGrid1: TDBGrid;DataSource1: TDataSource;Sou: TSpeedButton;shan: TSpeedButton;xiao: TSpeedButton;wei: TSpeedButton;zhuan: TSpeedButton;Sta: TStatusBar;Panel1: TPanel;Panel2: TPanel;Edit1: TEdit;Edit2: TEdit;SpeedButton1: TSpeedButton;CheckBox1: TCheckBox;procedure Edit1KeyPress(Sender: TObject; var Key: Char);procedure FormCreate(Sender: TObject);procedure xiaoClick(Sender: TObject);procedure SouClick(Sender: TObject);procedure shanClick(Sender: TObject);procedure weiClick(Sender: TObject);procedure zhuanClick(Sender: TObject);private{ Private declarations }public{ Public declarations }function sumrecordpage(rpage:integer):integer;procedure fenye;//sql语句分页procedure souye; //首页procedure buttonzt(a,b,c,d:boolean);//按钮状态procedure xiaoye; //下页procedure shanye;//上页procedure weiye;//尾页procedure danqiansta;//procedure tiaoye;//跳到end;varForm1: TForm1;tmp,psize,dye,youye:integer;//psize一页多少记录,dye现在的当前页是多少,youye是总页数,tmp是多少条记录implementation{$R *.dfm}procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);beginif not (key in ['0'..'9',#8]) then key:=#0;end;procedure TForm1.fenye;varsql:string;beginif dye=1 then beginsql:='select top '+inttostr(psize)+' * from 进药表 where in_id>(select max (in_id) from (select top '+inttostr(psize)+' in_id from 进药表 order by in_id) as tabtmp) order by in_id';adoq.Close;adoq.SQL.Clear; adoq.SQL.Add(sql);adoq.Open;end elsebeginsql:='select top '+inttostr(psize)+' * from 进药表 where in_id>(select max (in_id) from (select top '+inttostr(psize*dye)+' in_id from 进药表 order by in_id) as tabtmp) order by in_id';adoq.Close;adoq.SQL.Clear; adoq.SQL.Add(sql);adoq.Open;end;end;function TForm1.sumrecordpage(rpage: integer): integer;//rpage 每页多少条记录 返回多少页varsumado:tadoquery;beginsumado:=tadoquery.Create(self); sumado.Connection:=adoconn; with sumado dobeginclose; sql.Clear; sql.Add('select in_id from 进药表');open;tmp:=recordcount;if tmp=0 then beginresult:=1;end else begin if (tmp mod rpage)=0 then result:=(tmp div rpage) else result:= (tmp div rpage)+1;end;close; free;end;end;procedure TForm1.FormCreate(Sender: TObject);beginpsize:=10;dye:=0;youye:=sumrecordpage(psize);//有多少页souye;sta.Panels[0].Text:=' 共有'+inttostr(youye)+'页 '+inttostr(tmp)+'条记录';end;procedure TForm1.souye;beginif dye=0 then begin adoq.Close; adoq.SQL.Clear; adoq.SQL.Add('select top '+inttostr(psize)+' * from 进药表 order by in_id'); adoq.Open; if adoq.RecordCount<psize then buttonzt(false,false,false,false) else buttonzt(false,false,true,true);end elsebeginfenye;end;danqiansta;end;procedure TForm1.buttonzt(a, b, c, d: boolean);beginsou.Enabled:=a;shan.Enabled:=b;xiao.Enabled:=c;wei.Enabled:=d;end;procedure TForm1.xiaoye;begininc(dye);fenye;if dye=youye-1 then buttonzt(true,true,false,false) else buttonzt(true,true,true,true);end;procedure TForm1.xiaoClick(Sender: TObject);beginxiaoye;danqiansta;end;procedure TForm1.SouClick(Sender: TObject);begindye:=0;souye;end;procedure TForm1.shanye;begindec(dye);if dye=0 then souye else fenye;if not (dye=0) then buttonzt(true,true,true,true);end;procedure TForm1.shanClick(Sender: TObject);beginshanye;danqiansta;end;procedure TForm1.weiye;begindye:=youye-1;fenye;buttonzt(true,true,false,false);end;procedure TForm1.weiClick(Sender: TObject);beginweiye;danqiansta;end;procedure TForm1.danqiansta;beginsta.Panels[1].Text:=' 当前是第'+inttostr(dye+1)+'页';end;procedure TForm1.tiaoye;vartstr:integer;beginif edit1.Text='' thenbegin messagedlg('页数不能为空',mtWarning,[mbOK],0); exit;end;tstr:=strtoint(trim(edit1.Text)); if tstr>youye then begin messagedlg('输入的页数不能大于总页数',mtWarning,[mbOK],0); edit1.Text:=''; edit1.SetFocus; exit;end;dye:=tstr-1;if tstr=1 thenbegindye:=0;souye;danqiansta;end else beginfenye;danqiansta;if dye=youye-1 then buttonzt(true,true,false,false) else buttonzt(true,true,true,true);end;end;procedure TForm1.zhuanClick(Sender: TObject);begintiaoye;end;end.

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台