在 Delphi XE6 中使用 Datasnap 流式传输 TClientDataSet

Streaming TClientDataSet using Datasnap in Delphi XE6

我正在尝试使用 Delphi XE6 中的 Datasnap 流式传输 TClientDataSet。但是,我在客户端代码中不断收到 "Missing Data Provider or Data Packet" 错误。

//客户端

procedure TForm2.Button1Click(Sender: TObject);
var
 CDS: TClientDataSet;
 S: TStream;
begin
  CDS := TClientDataSet.Create(nil);
  try
    S:= ClientModule1.ServerMethods1Client.getCDSData;
    S.Seek(0,soFromBeginning);
    S.Position:= 0;
    CDS.LoadFromStream(S);
    CDS.Open; // Missing Data Provider or Data Packet
  finally
    CDS.Free;
  end;
end;

//服务器

function TServerMethods1.getCDSData: TStream;
var
  Writer: TBinaryWriter;
  CDS: TClientDataSet;
  I: Integer;
begin
  result := TMemoryStream.Create;
  CDS := TClientDataSet.Create(nil);
  with CDS.FieldDefs do
  begin
    Clear;
    Add('First', ftString, 20);
    Add('Last', ftString, 25);
  end;
  CDS.CreateDataSet;
  CDS.Open;
  CDS.AppendRecord(['John', 'Smith']);
  CDS.AppendRecord(['Jane', 'Doe']);
  try
    CDS.SaveToStream(result);
  finally
    CDS.Free;
  end;
end;

我还尝试将其流式传输为 XML 而不是二进制

  CDS.SaveToStream(result, dfXML);

得到同样的错误

"Missing Data Provider or Data Packet"

答案:

CDS.SaveToStream(result);
Result.Position := 0; //need this in server method "getCDSData"
CDS.SaveToStream(result);
Result.Position := 0;