用TClientDataSet结合TQuery来完成,但是要求你的数据表中要有一个唯一索引字段,最好是自增字段 我上面的例子用于分布式程序的(有几个小函数需要自己写: 1。InterSales.qurrecords----一个接口函数用于根据SQL获取记录,需要自己修改 2。GetKeyFieldValue---获得当前查询记录的最大的自增ID;), 还可以用于C/S。速度还可以,1000条记录在本机上可以和C/S的程序相比,在INTERBET上比C/S的快。用于C/S时,对源程序不用做大幅度的修改,只要修改DBGRID的DATASOURCE的属性指向 代码如下 Function GetNextData(CdsGridview:TClientDataSet;EdtView:TEdit;sTable,sField,sWhere,sTop:String;Var LocalCount:Integer):Boolean; //获取下一包数据 Var Odata:OleVariant; sSql:String; begin Result:=False; screen.Cursor := crhourglass; With CdsGridview do Begin if Not Active then Exit; sSql:='Select Top '+sTop+sField +' From '+sTable+' Where 1=1 '+sWhere; Odata:=null; InterSales.qurrecords(sSql,Odata); DisableControls; if Not VarisNull(Odata) then begin if Active then Close; Open; AppendData(Odata,true); Odata:=null; //得到此次查询的键值 LocalCount := GetKeyFieldValue('ID',CdsGridView); EnableControls; EdtView.Clear; EdtView.Text:='当前下载记录:'+IntToStr(CdsGridView.RecordCount)+'条'; Result:=True; end else begin EnableControls; screen.Cursor := crdefault; exit; end; screen.Cursor := crdefault; End; end; --------------------GetKeyFieldValue的函数原形: function GetKeyFieldValue(sortField:string;clientdb:TClientDataSet): OleVariant; var aCDS : TClientDataSet; begin aCDS:=TClientDataSet.Create(nil); try try if not varisnull(clientdb.Data) then begin ///如果重新按其他列排序后,此处可能不对了 ! aCDS.Data:=clientdb.Data; WITH acds do begin IndexName := ''; IndexDefs.Update; end; with aCDs.IndexDefs.AddIndexDef do begin Fields := sortField; Options := []; end; aCDs.IndexDefs.Update; aCDS.Last; Result:=aCDS.FieldByName(sortfield).Value; end; finally aCDS.Free ; end; except on e: Exception do raise; end; end;
分页怎么设置啊?
在查询之前设置DataSource为空
在查询之后再关联上!或者你的数据库中表设计的有没有问题,有没有建索引!
我上面的例子用于分布式程序的(有几个小函数需要自己写:
1。InterSales.qurrecords----一个接口函数用于根据SQL获取记录,需要自己修改
2。GetKeyFieldValue---获得当前查询记录的最大的自增ID;),
还可以用于C/S。速度还可以,1000条记录在本机上可以和C/S的程序相比,在INTERBET上比C/S的快。用于C/S时,对源程序不用做大幅度的修改,只要修改DBGRID的DATASOURCE的属性指向
代码如下
Function GetNextData(CdsGridview:TClientDataSet;EdtView:TEdit;sTable,sField,sWhere,sTop:String;Var LocalCount:Integer):Boolean; //获取下一包数据
Var
Odata:OleVariant;
sSql:String;
begin
Result:=False;
screen.Cursor := crhourglass;
With CdsGridview do
Begin
if Not Active then Exit; sSql:='Select Top '+sTop+sField
+' From '+sTable+' Where 1=1 '+sWhere;
Odata:=null;
InterSales.qurrecords(sSql,Odata);
DisableControls;
if Not VarisNull(Odata) then
begin
if Active then Close;
Open;
AppendData(Odata,true);
Odata:=null;
//得到此次查询的键值
LocalCount := GetKeyFieldValue('ID',CdsGridView);
EnableControls;
EdtView.Clear;
EdtView.Text:='当前下载记录:'+IntToStr(CdsGridView.RecordCount)+'条';
Result:=True;
end else
begin
EnableControls;
screen.Cursor := crdefault;
exit;
end;
screen.Cursor := crdefault;
End;
end;
--------------------GetKeyFieldValue的函数原形:
function GetKeyFieldValue(sortField:string;clientdb:TClientDataSet): OleVariant;
var
aCDS : TClientDataSet;
begin
aCDS:=TClientDataSet.Create(nil);
try
try
if not varisnull(clientdb.Data) then
begin
///如果重新按其他列排序后,此处可能不对了 !
aCDS.Data:=clientdb.Data;
WITH acds do begin
IndexName := '';
IndexDefs.Update;
end;
with aCDs.IndexDefs.AddIndexDef do
begin
Fields := sortField;
Options := [];
end;
aCDs.IndexDefs.Update;
aCDS.Last;
Result:=aCDS.FieldByName(sortfield).Value;
end;
finally
aCDS.Free ;
end;
except
on e: Exception do
raise;
end;
end;
谁有只设属性就可以解决的吗?
每次显示一页数据..呵呵..
2.在open前关闭对DataSource的显示,ADOQuery.disable,在open后再打开,ADQuery.enable,可避免grid不停止的刷新数据。