DataSnap – Tparams를 이용한 멀티 테이블 트랜잭션 업데이트

2687 단어 Delphi
서비스 포트:
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

좋은 웹페이지 즐겨찾기