DataSnap——利用TParams进行多表事务更新

2017-08-19 09:16:28来源:CSDN作者:xieyunc人点击

分享

服务端:

function TSVRDM.multUpdatesByPar(UpdateParam: TParams; out ErrMsg: string): OleVariant;const  aSQL = 'Select * from %s where 1<>1';var  i: Integer;  lQuery: TADOQuery;  lProvider: TDataSetProvider;  conn: TADOConnection;  ErrorCount, MaxErrors: Integer;begin  Writeln(fguid + ':Mult-Table Update start ...');  conn := ConnPool.Lock(dbConnStr);  lQuery := TADOQuery.Create(nil);  lProvider := TDataSetProvider.Create(nil);  Writeln(fguid + ': Start Transaction... ');  conn.BeginTrans;  try    try      lQuery.Connection := conn;      lProvider.DataSet := lQuery;      for I := 0 to UpdateParam.Count - 1 do begin        Writeln(fguid + ': Update Table ' + UpdateParam[i].Name);        lQuery.Close;        lQuery.SQL.Clear;        lQuery.SQL.Text := Format(aSQL, [UpdateParam[i].Name]);        result := lProvider.ApplyUpdates(UpdateParam[i].AsBytes, 0, ErrorCount);        ErrMsg := FErrMsg;        if ErrorCount > 0 then          raise Exception.Create(ErrMsg + '; TableName = ' + UpdateParam[i].Name);      end;      conn.CommitTrans;      Writeln(fguid + ': Transaction commited... Update finished!');    except      on E: Exception do      begin        conn.RollbackTrans;        Writeln(fguid + ':Transaction rollbacked! Update ERROR :' + E.Message);      end;    end;  finally    lProvider.Free;    lQuery.Free;    ConnPool.Unlock(conn);    FErrMsg := '';  end;end;
客户端:

procedure TMyClient.btn1Click(Sender: TObject);var  aParams: TParams;  aPar: TParam;  client: TDMClient;  ErrMsg: string;begin// TClientDataSet的Data和Delta可用TParam.AsBytes直接传递  aParams := TParams.Create(nil);  if ClientDataSet1.ChangeCount > 0 then  begin    aPar := aParams.CreateParam(ftVarBytes, '订单表', ptInput);//有可能是主表    aPar.AsBytes := ClientDataSet1.Delta;  end;  if ClientDataSet2.ChangeCount > 0 then  begin    aPar := aParams.CreateParam(ftVarBytes, '订单明细表', ptInput);//有可能是从表    aPar.AsBytes := ClientDataSet2.Delta;  end;  if aParams.Count = 0 then    exit;  client := TDMClient.Create(self.SQLConn.DBXConnection);  try    try      client.multUpdatesByPar(aParams, ErrMsg);      if ErrMsg <> '' then        raise Exception.Create(ErrMsg)      else      begin        ClientDataSet1.MergeChangeLog;        ClientDataSet2.MergeChangeLog;      end;    except      on E: Exception do      begin        showmessage(E.Message);      end;    end;  finally    client.Free;  end;end;

转自:http://www.cnblogs.com/hnxxcxg/p/6897116.html

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台