[译]FieldByName速度优化

  • 主题发起人 SUNSTONE的Delphi笔记
  • 开始时间
S

SUNSTONE的Delphi笔记

Unregistered / Unconfirmed
GUEST, unregistred user!
调用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;

作者:sunstone 发表于 2011/01/23 08:13:00 原文链接 //blog.csdn.net/sunstone/article/details/6159433
阅读:2657

查看更多...
 

Similar threads

S
回复
0
查看
582
SUNSTONE的Delphi笔记
S
S
回复
0
查看
701
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
611
import
I
顶部