例用AdoQuery分页

这篇博客展示了如何在Delphi中利用AdoQuery组件进行分页查询。在FormCreate事件中设置AdoConnection1的连接字符串并打开连接。Page_Count函数用于填充TListView,每次显示10条记录,并在Button1Click和Button2Click事件中实现翻页功能,更新显示的页数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; /* 336x280, 创建于 07-11-21 */ google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 首先在窗体上放个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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值