【问题标题】:How to (really) empty TClientDataSet如何(真正)清空 TClientDataSet
【发布时间】:2018-09-13 14:32:46
【问题描述】:

我使用 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'方法会删除所有记录,但是当我保存到文件时,文件大小仍然保持不变!如何真正擦除所有数据和文件大小?

谢谢

【问题讨论】:

  • 我无法重现所描述的行为。
  • myClientDataSet.ApplyUpdates?
  • 注意表结构还是要写的。

标签: delphi datasnap


【解决方案1】:

当我保存到文件时,文件大小仍然保持不变!如何真正擦除所有数据和文件大小?

使用我自己的测试 .CDS 文件,我无法重现您的结果 - 请参阅 cmets 在以下代码中,显示在EmptyDataSet 之后,(重新)保存的文件具有合理的、小得多的大小。

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;

所以如果你保存的文件真的是同一个文件和文件大小,那一定是因为 你在q中没有告诉我们的事情。尝试调用

myClientDataSet.Close;
myClientDataSet.Open; 

之后

myClientDataSet.CreateDataSet;

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 2019-04-03
    相关资源
    最近更新 更多