如何 return 具有嵌套数据集的数据集从 datasnap 服务器到客户端?
How to return a dataset with nested datasets from datasnap server to client?
我用Delphi柏林。我有一个示例 datasnap 服务器 return 一个数据集到客户端,该数据集包含 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服务器return出错未实现功能。任何想法如何return数据集包含从 datasnap 服务器到客户端的嵌套数据集?
我使用 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
我找到了解决方案。
服务器方法:
function TServerMethods1.GetDataSet: OleVariant;
//...
begin
D := TClientDataSet.Create(nil);
try
//...
Result := D.Data;
finally
D.DisposeOf;
end;
end;
客户:
procedure TForm1.Button1Click(Sender: TObject);
var C: TServerMethods1Client;
begin
C := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
try
ClientDataSet1.Data := C.GetDataSet;
finally
C.DisposeOf;
end;
end;
我用Delphi柏林。我有一个示例 datasnap 服务器 return 一个数据集到客户端,该数据集包含 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服务器return出错未实现功能。任何想法如何return数据集包含从 datasnap 服务器到客户端的嵌套数据集?
我使用 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
我找到了解决方案。
服务器方法:
function TServerMethods1.GetDataSet: OleVariant;
//...
begin
D := TClientDataSet.Create(nil);
try
//...
Result := D.Data;
finally
D.DisposeOf;
end;
end;
客户:
procedure TForm1.Button1Click(Sender: TObject);
var C: TServerMethods1Client;
begin
C := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
try
ClientDataSet1.Data := C.GetDataSet;
finally
C.DisposeOf;
end;
end;