Streaming TClientDataSet using Datasnap in Delphi

2019-07-16 03:49发布

问题:

I am trying to stream a TClientDataSet using Datasnap in Delphi XE6. However, I keep getting a "Missing Data Provider or Data Packet" error on the client side code.

//Client

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;

//Server

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;

I also tried Streaming it as XML instead of Binary

  CDS.SaveToStream(result, dfXML);

get the same error

"Missing Data Provider or Data Packet"

ANSWER:

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

回答1:

CDS.SaveToStream(result);
Result.Position := 0;