事务执行SQL语句

// ******************************************************************************
    // 函数功能: 事务执行SQL语句列表。
    // 函数名称: cf_dbExeSQLsTrans
    // 函数参数: oADOConnection      TADOConnection 数据库连接对象。
    // oSqls               TStringList     数据库连接字符串
    // 返回值: 返回是否操作成功(Boolean);
    // ******************************************************************************
    function cf_dbExeSQLsTrans(sSqls: String;
      oADOConnection: TADOConnection = nil): Boolean; overload;
    var
      iSqls, iCount: Integer;
      oADOQuery: TADOQuery;
      _oADOConnection: TADOConnection;
      sSQL, sException: string;
    begin
      Result := false;
      sException := '';
      _oADOConnection := TADOConnection.Create(nil);
      if (oADOConnection = nil) or (Trim(oADOConnection.ConnectionString) = '')
        then
      begin
        if (goADOConDef <> nil) and (Trim(goADOConDef.ConnectionString) <> '')
          then
        begin
          oADOConnection := goADOConDef;
        end
        else
        begin
          Result := false;
          Exit;
        end;
      end
      else
      begin
        if (goADOConDef <> nil) and (Trim(goADOConDef.ConnectionString) = '')
          then
        begin
          if goADOConDef.Connected then
            goADOConDef.Connected := false;
          goADOConDef.ConnectionString := oADOConnection.ConnectionString;
          goADOConDef.LoginPrompt := false;
          if not goADOConDef.Connected then
            goADOConDef.Open;
        end
        else if (goADOConDef = nil) and
          (Trim(goADOConDef.ConnectionString) = '') then
        begin
          goADOConDef := TADOConnection(Application);
          goADOConDef.LoginPrompt := false;
          goADOConDef.ConnectionString := oADOConnection.ConnectionString;
          if not goADOConDef.Connected then
            goADOConDef.Open;
        end;
      end;

      if oADOConnection = nil then
      begin
        Result := false;
        Exit;
      end;

      if _oADOConnection.Connected then
        _oADOConnection.Close;
      _oADOConnection.ConnectionString := oADOConnection.ConnectionString;
      _oADOConnection.ConnectionTimeout := 2000;
      _oADOConnection.LoginPrompt := false;
      // 完全隔离级别。
      _oADOConnection.IsolationLevel := ilSerializable;
      _oADOConnection.Open;

      // 数据库查询语句
      if (sSqls = '') and (Pos(';', sSqls) <= 0) then
        Exit;
      //
      sSqls := sSqls + ';';
      iSqls := cf_valGetStrPosNum(sSqls, ';') - 1;

      oADOQuery := TADOQuery.Create(nil);
      try
        try
          oADOQuery.Connection := _oADOConnection;
          _oADOConnection.BeginTrans;
          for iCount := 1 to iSqls do
          begin
            try
              if oADOQuery.Active then
                oADOQuery.Close;
              oADOQuery.SQL.Clear;
              sSQL := cf_valGetStrPosStr(sSqls, iCount, ';');
              // 不存在Sql语句。
              if Trim(sSQL) = '' then
                Continue;
              oADOQuery.SQL.ADD(sSQL);
              oADOQuery.ExecSQL;
            except
              on E: Exception do
              begin
                sException := E.Message;
               _oADOConnection.RollbackTrans;
                cf_sysLog('事务执行SQL语句列表函数过程失败,引起会滚! 错误位置:[' +
                    'function cf_dbExeSQLsTrans(sSqls: String;oADOConnection: TADOConnection = nil): Boolean; overload;] 执行SQL:[' + sSQL + '] 引发异常:[' + sException + ']');
                Exit;
              end;
            end;
          end;
          Result := true;
          _oADOConnection.CommitTrans;
        except
          _oADOConnection.RollbackTrans;
          cf_sysLog('事务执行SQL语句列表函数过程失败,引起会滚! 错误位置:[' +
              'function cf_dbExeSQLsTrans(sSqls: String;oADOConnection: TADOConnection = nil): Boolean; overload;]');
        end;
      finally
        if oADOQuery.Active then
          oADOQuery.Close;
        if _oADOConnection.Connected then
          _oADOConnection.Close;
        oADOQuery.Free;
        _oADOConnection.Free;
      end;
    end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值