/// <summary> /// 执行 SQL 命令 /// </summary> protected virtual T DoExecute <T>(IDbCommand command, Func <IDbCommand, T> func, bool wasClosed, bool interceptException = true) { T TResult = default(T); try { if (command.Transaction != null && _transaction != null && command.Transaction != _transaction) { throw new XFrameworkException("DoExecute: IDbCommand.Transaction does not equals to current transaction."); } if (command.Connection != null && _connection != null && command.Connection != _connection) { throw new XFrameworkException("DoExecute: IDbCommand.Connection does not equals to current connection."); } if (command.Connection == null) { command.Connection = this.CreateConnection(); } if (command.Connection.State != ConnectionState.Open) { if (string.IsNullOrEmpty(command.Connection.ConnectionString)) { command.Connection.ConnectionString = _connString; } command.Connection.Open(); } if (interceptException) { DbInterception.OnExecuting(command); } TResult = func(command); if (interceptException) { DbInterception.OnExecuted(command); } } catch (DbException e) { // 外层不捕获异常,由内层Func去捕获 if (interceptException) { DbInterception.OnException(new DbCommandException(e.Message, e, command)); } throw; } finally { if (command != null) { command.Dispose(); } if (wasClosed) { this.InternalDispose(); } } return(TResult); }