如何将带有嵌套数据集的数据集从datasnap服务器返回到客户端?

时间:2017-03-13 03:00:42

标签: delphi datasnap

我使用Delphi Berlin。我有一个示例datasnap服务器将数据集返回给客户端,该数据集包含TDataSetField。

服务器方法:

unit ServerMethodsUnit1;

//...

implementation

uses Datasnap.DBClient;

function TServerMethods1.GetDataSet: TDataSet;
var D: TClientDataSet;
    D1: TDataSet;
    F: TFieldDef;
begin
  D := TClientDataSet.Create(nil);
  D.FieldDefs.Add('DocKey', ftLargeint);
  D.FieldDefs.Add('DocNo', ftWideString, 20);
  D.FieldDefs.Add('Detail', ftDataSet);
  F := D.FieldDefs.Find('Detail');
  F.ChildDefs.Add('DtlKey', ftLargeint);
  F.ChildDefs.Add('DocKey', ftLargeint);
  F.ChildDefs.Add('Amount', ftBcd, 2);
  D.CreateDataSet;

  D.Append;
  D.FindField('DocKey').AsLargeInt := 1;
  D.FindField('DocNo').AsString := 'IV-00001';
  D1 := (D.FindField('Detail') as TDataSetField).NestedDataSet;
  D1.AppendRecord([1, 1, 123.12]);
  D1.AppendRecord([2, 1, 234.23]);
  D.Post;
  Result := D;
end;

end.

客户:

unit Unit1;

//...

procedure TForm1.Button1Click(Sender: TObject);
var C: TServerMethods1Client;
    D: TDataSet;
begin
  C := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
  try
    D := C.GetDataSet;
    //...
  finally
    C.DisposeOf;
  end;
end;

当客户端调用TServerMethods1Client.GetDataSet方法时,datasnap服务器返回错误未实现的功能。有关如何返回数据集的任何想法都包含从datasnap服务器到客户端的嵌套数据集?

2 个答案:

答案 0 :(得分:2)

我找到了解决方案。

服务器方法:

BUCKET_ID START_DATE END_DATE
1         13-MAR-17  19-Mar-17
2         20-MAR-17  26-Mar-17
3         27-Mar-17  02-APr-17  

客户:

function TServerMethods1.GetDataSet: OleVariant;
//...
begin
  D := TClientDataSet.Create(nil);
  try
    //...   
    Result := D.Data;
  finally
    D.DisposeOf;
  end;
end;

答案 1 :(得分:0)

我使用DataSnap REST Application(服务器)和DataSnap REST Client (Client)。您可以使用 TFDJSONDataSets (FireDac Reflect)

function TServerMethods1.GetDataSet: TFDJSONDataSets;
var 
  D: TClientDataSet;
  D1: TDataSet;
  F: TFieldDef;
begin
  D := TClientDataSet.Create(nil);
  D.FieldDefs.Add('DocKey', ftLargeint);
  D.FieldDefs.Add('DocNo', ftWideString, 20);
  D.FieldDefs.Add('Detail', ftDataSet);
  F := D.FieldDefs.Find('Detail');
  F.ChildDefs.Add('DtlKey', ftLargeint);
  F.ChildDefs.Add('DocKey', ftLargeint);
  F.ChildDefs.Add('Amount', ftBcd, 2);
  D.CreateDataSet;

  D.Append;
  D.FindField('DocKey').AsLargeInt := 1;
  D.FindField('DocNo').AsString := 'IV-00001';
  D1 := (D.FindField('Detail') as TDataSetField).NestedDataSet;
  D1.AppendRecord([1, 1, 123.12]);
  D1.AppendRecord([2, 1, 234.23]);
  D.Post;

  TFDJSONDataSetsWriter.ListAdd(Result,'DATASET',D);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 LDataSetList: TFDJSONDataSets;
 LDataSet: TFDDataSet;
begin     
  try
    LDataSet := TFDJSONDataSetsReader.GetListValueByName(LDataSetList, 'D');    
    if (oFDMemTable.Active) then
      oFDMemtable.Close;
    oFDMemTable.AppendData(LDataSet);
  except
    on E: TDSRestProtocolException do
      HandleRestException(ClientModule1.DSRestConnection, 'Error', E)
    else
      raise;
  end;  
end;

我传递了一些例子: http://docwiki.embarcadero.com/Libraries/Berlin/en/Data.FireDACJSONReflect.TFDJSONDataSets http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_a_REST_DataSnap_Server_with_an_Application_and_FireDAC