我使用TClientDataSet来存储一些本地数据,使用'savetofile'方法。
当我使用下面的代码时,我注意到了:
// at this point the file myfile.cds has 3000 records and 130 kbytes.
myClientDataSet.loadfromfile('myfile.cds');
myClientDataSet.emptydataset;
myClientDataSet.savetofile('myfile.cds');
// at this point the file myfile.cds has no records anymore, but still has the same size, 130kb !
似乎'emptydataset'方法将删除所有记录,但是当我保存到文件时,文件大小仍然保持不变!如何真正删除所有数据和文件大小?
谢谢
当我保存到文件时,文件大小仍然保持不变!如何真正删除所有数据和文件大小?
使用我自己的测试.CDS文件,我无法重现您的结果 - 请参阅以下代码中的注释,这些注释表明在qazxswpoi之后,(重新)保存的文件具有合理的,小得多的大小。
EmptyDataSet
因此,如果您保存的文件实际上是同一个文件和文件大小,那一定是因为您在q中没有告诉我们的内容。试着打电话
procedure TForm1.FormCreate(Sender: TObject);
begin
EmptyDataSetTest;
end;
procedure TForm1.EmptyDataSetTest;
var
SourceFN,
DestFN : String;
begin
SourceFN := ExtractFilePath(Application.ExeName) + '\Data\CDSData.Cds';
// SourceFN file is 333709 bytes acc to a Dir command in a CMD window
DestFN := ExtractFilePath(Application.ExeName) + '\Data\DestCDSData.Cds';
myClientDataSet.LoadFromFile(SourceFN);
myClientDataSet.EmptyDataSet;
myClientDataSet.SaveToFile(DestFN);
// DestFN file is 2268 acc to a Dir command in a CMD window
DestFN := ChangeFileExt(DestFN, '.XML');
myClientDataSet.SaveToFile(DestFN, dfXML);
// .XML file is 5128 and the file contains only the field
// FIELD descriptor tags under the \DATAPACKET\METADATA\FIELDS node
end;
后
myClientDataSet.Close;
myClientDataSet.Open;