我有一个应用程序,它使用内存表中的客户端数据集,以及从实验室仪器接收到的数据的本地存档。随着时间的推移,我向这些客户端数据集添加了一些字段,并且为了使保存的数据保持可访问性,我编写了一个小程序来更新旧的保存文件。到目前为止,它一直运行良好。最近,我不得不使用嵌套数据集,我尝试更改我的代码以迭代嵌套数据集并更新它们。到目前为止我想出的代码是这样的:
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 秒。是否有更快的方法来更新客户端数据集?