我有一个应用程序,它使用客户端数据集,如内存表,以及本地存档,用于从实验室仪器接收的数据。随着时间的推移,我为这些客户端数据集添加了一些字段,为了保持可访问的已保存数据,我编写了一个小程序,它实际上更新旧的已保存文件。到目前为止,它一直运作良好。最近,我不得不使用嵌套数据集,我试图改变我的代码来迭代嵌套数据集,并且更新它们也是如此。到目前为止我提出的代码是:
procedure UpdateCDS(NewCDS: TClientDataSet; const OldFileName: string);
procedure InternalUpdateCDS(NewCDS, OldCDS: TClientDataSet);
var
i, j: Integer;
NewNestedCDS, OldNestedCDS: TClientDataSet;
begin
OldCDS.First;
while not OldCDS.Eof do
begin
NewCDS.Append;
for i := 0 to NewCDS.FieldDefs.Count - 1 do
if NewCDS.Fields[i].FieldKind = fkData then
for j := 0 to OldCDS.Fields.Count - 1 do
if UpperCase(NewCDS.Fields[i].FieldName)
= UpperCase(OldCDS.Fields[j].FieldName) then
begin
if not OldCDS.Fields[j].IsNull then
if OldCDS.Fields[j].DataType <> ftDataSet then
NewCDS.Fields[i].Value := OldCDS.Fields[j].Value
else
begin
NewNestedCDS := TClientDataSet.Create(nil);
try
OldNestedCDS := TClientDataSet.Create(nil);
try
OldNestedCDS.DataSetField :=
TDataSetField(OldCDS.Fields[j]);
NewNestedCDS.DataSetField :=
TDataSetField(NewCDS.Fields[i]);
OldNestedCDS.Open;
NewNestedCDS.Open;
InternalUpdateCDS(NewNestedCDS, OldNestedCDS);
finally
OldNestedCDS.Free;
end;
finally
NewNestedCDS.Free;
end;
end;
Break;
end;
NewCDS.Post;
OldCDS.Next;
end;
NewCDS.MergeChangeLog;
end;
var
OldCDS: TClientDataSet;
begin
OldCDS := TClientDataSet.Create(nil);
try
OldCDS.LoadFromFile(OldFileName);
InternalUpdateCDS(NewCDS, OldCDS);
finally
OldCDS.Free;
end;
end;
我的问题是,这段代码太慢了。对于1000条记录,我的intel T7500大约需要10秒钟。是否有更快的更新客户端数据集的方法?