例用AdoQuery分页

类别:Delphi 点击:0 评论:0 推荐:

今晚在CSDN论坛上看到一个网友在问分页,就随便写了一段代码,如有不当之处,还请见谅。

首先在窗体上放个listview,两个button,一个label,一个adoquery,一个adoconnection
然后用adoconnection建立一个连接

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB,Grids, DBGrids, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    ListView1: TListView;
    Label1: TLabel;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    procedure Page_Count;
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  page: integer;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  ListItem:TListItem;
  i:integer;
begin
  with AdoConnection1 do
  begin
    ConnectionString := '连接串';
    open();
  end;
  Page := 0;
end;

procedure TForm1.Page_Count;
var
  i:integer;
  ListItem:TListItem;
begin
  ListView1.Clear;
  with AdoQuery1 do
  begin
    Connection := AdoConnection1;
    sql.Clear;
    sql.Add('select a,b from tmplive');
    open;
    if Page = 0 then Page := 1;
    if Page > RecordSet.RecordCount then page := RecordSet.RecordCount;
    Recordset.PageSize := 10;
    RecordSet.AbsolutePage := Page;
    for i:= 1 to Recordset.PageSize do
    begin
      ListItem := ListView1.Items.Add;
      ListItem.SubItems.Add(RecordSet.Fields.Item[0].value);
      ListItem.SubItems.Add(RecordSet.Fields.Item[1].Value);
      Next;
      if RecordSet.EOF then exit;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Page := Page+1;
  Page_Count;
  Label1.Caption := IntToStr(Page);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    Page := Page -1;
    Page_Count;
    Label1.Caption := IntToStr(Page);
end;

end.

本文地址:http://com.8s8s.com/it/it5229.htm