FieldByName速度优化

调用FieldByName主要是调用FindField,会在主循环引进n个新子循环,如果有1万个记录,那效率会可想而知。

下面优化比较好

var
AField : TField;

begin
Assert(AdoQuery1.Active, 'Dataset is not active!');
try
AdoQuery1.DisableControls;
AField := AdoQuery1.FieldByName('MyFieldName');

AdoQuery1.First;
while not AdoQuery1.Eof do
begin
AdoQuery1.Edit;
AField.Value := Edit1.Text;

AdoQuery1.Post;
AdoQuery1.Next;
end;
finally
AdoQuery1.EnableControls;
end;
end;

如果有多个字段,想简化一下

const ROWCOUNT = 1000000;
var
k, j: Integer;
FieldList: iFieldList;
begin
FieldList := GetFieldList(ClientDataSet1);

ClientDataSet1.Active := False;
ClientDataSet1.Active := True;
for j := 1 to ROWCOUNT do
begin
ClientDataSet1.Append;
for k := 1 to 10 do
FieldList.FieldByName('ClientDataSet1Field' + IntToStr(k)).AsInteger := k;
ClientDataSet1.Post;
end;
end;

posted @ 2013-01-06 16:03  硕霆  阅读(341)  评论(0编辑  收藏  举报