FIREDAC的TFDJSONDataSets和TFDJSONDeltas查询和提交数据

Posted 咏南中间件和开发框架

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FIREDAC的TFDJSONDataSets和TFDJSONDeltas查询和提交数据相关的知识,希望对你有一定的参考价值。

 

服务端代码:

uses 

Data.FireDACJSONReflect,
FireDAC.Stan.Storage, FireDAC.Stan.StorageBin, FireDAC.Stan.StorageJSON,
FireDAC.Stan.StorageXML;

1)查询

function TServerMethods1.QuerySql2(const accountNo, sql: string): TFDJSONDataSets;
var
d: TfrmDB;
begin
Result := nil;
if (accountNo = ‘‘) or (sql = ‘‘) then
Exit;
d := GetDBPool(accountNo).Lock;
if not Assigned(d) then
Exit;
try
try
d.qryOpen.Close;
d.qryOpen.sql.Clear;
d.qryOpen.sql.Text := sql;
d.qryOpen.Open;
Result := TFDJSONDataSets.Create;
TFDJSONDataSetsWriter.ListAdd(Result, ‘1‘, d.qryOpen);
except
on e: Exception do
begin
Result := nil;
Log.WriteLog(‘TServerMethods1.QuerySql2 ‘ + e.Message);
end;
end;
finally
d.qryOpen.Close;
GetDBPool(accountNo).Unlock(d);
end;
end;

2)提交

function TServerMethods1.SaveData2(const accountNo, tableName: string; delta: TFDJSONDeltas): Boolean;
var
d: TfrmDB;
LApply: IFDJSONDeltasApplyUpdates;
begin
Result := False;
if (accountNo = ‘‘) or (tableName = ‘‘) or (delta = nil) then
Exit;
d := GetDBPool(accountNo).Lock;
if not Assigned(d) then
Exit;
try
try
d.qryOpen.Close;
d.qryOpen.sql.Clear;
d.qryOpen.sql.Text := ‘select * from ‘ + tableName + ‘ where 1=2‘;
d.qryOpen.Open;
LApply := TFDJSONDeltasApplyUpdates.Create(delta);
LApply.ApplyUpdates(0, d.qryOpen.Command);
Result := LApply.Errors.Count = 0;
except
on e: Exception do
begin
Result := False;
Log.WriteLog(‘TServerMethods1.SaveData2 ‘ + e.Message);
end;
end;
finally
d.qryOpen.Close;
GetDBPool(accountNo).Unlock(d);
end;
end;

客户端代码:

uses

FireDAC.Stan.StorageJSON, FireDAC.Stan.StorageBin
,Data.FireDACJSONReflect, FireDAC.Stan.StorageXML ,FireDAC.Stan.Storage;

切记:

FDMemTable1.CachedUpdates := True;

1)查询

procedure TForm1.btnQueryClick(Sender: TObject);
var
r: TServerMethods1Client;
LDataSetList: TFDJSONDataSets;
LDataSet: TFDDataSet;
begin
r := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
LDataSetList := r.QuerySql2(‘0‘, ‘select * from t1‘);
LDataSet := TFDJSONDataSetsReader.GetListValueByName(LDataSetList,‘1‘);
FDMemTable1.Close;
FDMemTable1.AppendData(LDataSet);
r.Free;
end;

2)提交

procedure TForm1.btnSaveClick(Sender: TObject);
var
r: TServerMethods1Client;
d: TFDJSONDeltas;
begin
r := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
if FDMemTable1.State in dsEditModes then
FDMemTable1.Post;
if FDMemTable1.ChangeCount = 0 then
Exit;
d := TFDJSONDeltas.Create;
TFDJSONDeltasWriter.ListAdd(d, ‘1‘, FDMemTable1);
if r.SaveData2(‘0‘, ‘t1‘, d) then
Self.Caption := ‘save ok‘
else self.Caption := ‘save fail‘;
r.Free;
end;

以上是关于FIREDAC的TFDJSONDataSets和TFDJSONDeltas查询和提交数据的主要内容,如果未能解决你的问题,请参考以下文章

FireDAC 下的 Sqlite-控件直接添加代码窗体

FireDac Firebird和Android

使用 FireDac 在 Delphi 中动态创建和调用存储过程的正确方法是啥?

关于DBX Framewrok 和 FireDac 的一点随笔

如何找到用于 FireDac 连接的 MS Access 版本和/或 dll 名称?

Delphi:从 IBO 迁移到 FireDac