zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    Datesheet 参数手册
    2017.10.23 Arduino Atmel EFM32低功耗监测
    New Concept English three(21)
    The disadvantage for manager has a part-time job as a trainer
    New Concept English three(20)
    Python+Qt学习随笔:PyQt中常用的事件处理函数
    Python+Qt学习随笔:PyQt图形界面应用的事件处理流程
    PyQt学习遇到的问题:重写notify发送的消息为什么首先给了一个QWindow对象?
    PyQt学习随笔:PyQt中捕获键盘事件后获取具体按键值的方法
    PyQt学习随笔:重写组件的event方法捕获组件的事件
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/5500826.html
Copyright © 2011-2022 走看看