一、FireDAC调用DataSnap远程方法查询数据示例
1、服务端使用FDQUERY查询数据并返回TDATASET:
function TServerMethods1.GetData(var sqlstr: string): TDataSet;
var
qry:TFDQurey;
begin
qry := TFDQuery.Create(nil);
try
qry.Connection := TServerContainer1.FDConnection1;
qry.Open(sql);
Result := TFDMemTable.Create(nil);
TFDMemTable(Result).Data := qry.Data;
finally
qry.Free;
end;
end;
2、客户端既可以用FDConnection连接DataSnap中间层,也可以用SQLConnection连接DataSnap中间层,如果用FDConnection连接DataSnap,则可以用FDStoredProc访问远程方法,如:
procedure TForm1.Button1Click(Sender: TObject);
begin
FDConnection1.Connected := true;
FDStoredProc1.Close;
FDStoredProc1.Unprepare;
FDStoredProc1.StoredProcName := 'TServerMethods1.GetData';
FDStoredProc1.Prepare;
FDStoredProc1.ParamByName('sqlstr').Value := 'select * from bas_goods';
FDStoredProc1.Open;
FDMemTable1.Close;
FDMemTable1.Data := FDStoredProc1.Data;
FDStoredProc1.Close;
end;
二、使用FireDAC如何在客户端提交Delta数据
你可以在客户端序列FireDAC数据集的DELTA , 将序列后的Stream发送给中间件,中间件的TFDQuery或TFDMemTable调用LoadFromStream()方法加载流,然后调用ApplyUpdates()将数据保存进数据库中。
怎样转换TFDQuery或TFDMemTable的Delta为Stream呢?
首先,你需要设置FDQuery或TFDMemTable的ResourceOptions.StoreItems 为 [siDelta]或 [siMeta, siDelta]。
然后,调用SaveToStream()方法即可。下面是演示用的代码:
var
Stream:TMemoryStream;
begin
// qryDataSource have 100 records,modified 1 record,so have 1 Delta record
Stream := TMemoryStream.Create;
try
qryDataSource.ResourceOptions.StoreItems := [siData,siMeta,siDelta];
Stream := TMemoryStream.Create;
qryDataSource.SaveToStream(Stream);
//restore StoreItems
qryDataSource.ResourceOptions.StoreItems := [siData,siMeta,siDelta]; Stream.Position := 0;
// remote app,transports stream by http
qryRemote.Close;
qryRemote.CachedUpdates := True;
qryRemote.UpdateOptions.KeyFields := 'ID';
qryRemote.UpdateOptions.UpdateTableName := '上机记录表';
qryRemote.SQL.Text := 'select * from 上机记录表 where 1=0'; //qryRemote.ResourceOptions.StoreItems := [siMeta,siDelta,siData];
//It load 100 reocrds,not only 1 delta record
qryRemote.LoadFromStream(Stream);
// commit and refresh
qryRemote.ApplyUpdates();
qryDataSource.CommitUpdates;
qryDataSource.Refresh;
finally
Stream.Free;
end;
end;
版权声明:本文为博主原创文章,未经博主允许不得转载。