注意将STREAM写入ini文件的SECION时,每个节只能写1024字节。以下是我讲tclientDataSet写入INI文件节,以及从节中读出再恢复到CDS的函数,供你参考
Function SaveDataToFile(CDS:TClientDataSet; FileName:String):Boolean;
var ini:TIniFile;
tpVal:OleVariant;
tpStream,tpSt:TMemoryStream;
i:Integer;
begin
Result := False;
if CDS = Nil then
Abort;
if Not CDS.Active then
Abort;
if StrIsEmpty(FileName) then
Abort;
Ini:=TIniFile.Create(FileName);
tpStream:=TmemoryStream.Create;
tpSt:=TmemoryStream.Create;
try
if CDS.State in [DsEdit, dsInsert] then
CDS.Post;
CDS.SaveToStream(tpStream);
tpStream.Position :=0;
//分块写入数据,每块1000字符
For i := 0 to tpStream.Size div 1000 do begin
tpSt.Clear;
tpStream.Position := i*1000;
if (i+1)*1000 < tpStream.Size then
tpSt.CopyFrom(tpStream, 1000)
else
tpSt.CopyFrom(tpStream, tpStream.Size - i * 1000);
tpSt.Position := 0;
ini.WriteBinaryStream(CDS.Name,'Data'+IntToStr(i),tpSt);
end;
Result := True;
Finally
Ini.Free;
tpStream.Free;
tpSt.Free;
End;
end;
Function ReadDataFromFile(CDS:TClientDataSet; FileName:String):Boolean;
var ini:TIniFile;
tpStream:TMemoryStream;
tpList:TStrings;
i:Integer;
begin
Result := False;
if Not FileExists(FileName) then
Abort;
if CDS = Nil then
Abort;
Ini:=TIniFile.Create(FileName);
tpStream:=TmemoryStream.Create;
tpList := TStringList.Create;
try
if Ini.SectionExists(CDS.Name) then begin
Ini.ReadSection(CDS.Name, tpList);
tpStream.Clear;
For i:=0 to tpList.Count - 1 do
if Ini.ValueExists(CDS.Name,tpList.Strings) then begin
tpStream.Position := i * 1000;
ini.ReadBinaryStream(CDS.Name ,tpList.Strings,tpStream);
end;
Result := CopyDataToCDS(CDS,tpStream);
end;
Finally
tpStream.Free;
Ini.Free;
tpList.Free;
End;
end;