今晚在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.
-----------------------------------ADOquery循环
var
Lv_Count : Integer;
begin
Lv_Count := 0;
if not adoQuery1.isEmpty then
begin
adoQuery1.First;
while not adoQuery1.eof do
begin
Lv_Count := Lv_Count + 1;
adoQuery1.next;
end;
end;
end;