/// <summary> /// Allow ad-hoc queries or stored procedures to return an open data reader. /// </summary> /// <param name="sql">The SQL code or stored procedure name to execute</param> /// <param name="commandType">Type of command to execute</param> /// <param name="parameters">Parameter list requied by the statement or stored procedure</param> /// <returns>An open data reader</returns> public DbDataReader GetDataReader(string sql, CommandType commandType, params object[] parameters) { if (sql == null) { throw new ArgumentNullException("sql"); } if (_connection == null) { throw new InvalidOperationException("must set connection before calling"); } DbCommand command = _connection.CreateCommand(); command.CommandText = sql; command.CommandType = commandType; DbTransactionRegistry.SetCommandTransaction(command); if (_commandTimeout.HasValue) { command.CommandTimeout = _commandTimeout.Value; } if (parameters != null) { AddParamsToCommand(command, _dialect, parameters, _parameterPrefix); } return(command.ExecuteReader()); }
/// <summary> /// Create and initializes a new DbCommand object from the SP name and parameter list /// </summary> /// <param name="storedProcName">Name of the Stored Procedure to be called by the command</param> /// <param name="parameters">Parameter list required by the stored procedure</param> /// <returns>A <see cref="System.Data.Common.DbCommand"/> object with the command to execute</returns> public DbCommand GetCommandFromSP(string storedProcName, params object[] parameters) { if (storedProcName == null) { throw new ArgumentNullException("storedProcName"); } if (_connection == null) { throw new InvalidOperationException("must set connection before calling"); } DbCommand command = _connection.CreateCommand(); command.CommandText = storedProcName; command.CommandType = CommandType.StoredProcedure; if (_commandTimeout.HasValue) { command.CommandTimeout = _commandTimeout.Value; } DbTransactionRegistry.SetCommandTransaction(command); if (parameters != null) { AddParamsToCommand(command, _dialect, parameters, _parameterPrefix); } return(command); }
/// <summary> /// Enrolls a command in either the local transaction /// or the registered transaction for the connection /// </summary> /// <param name="command"></param> protected void EnrollInTransaction(DbCommand command) { if (Transaction != null) { command.Transaction = Transaction; } else { DbTransactionRegistry.SetCommandTransaction(command); } }
/// <summary> /// Calls sql command that returns single scalar value /// </summary> /// <param name="command">The command.</param> /// <returns>The first column of the first row in the result set.</returns> public object ExecuteScalar(DbCommand command) { if (command == null) { throw new ArgumentNullException("command"); } if (_connection == null) { throw new InvalidOperationException("must set connection before calling"); } command.Connection = _connection; DbTransactionRegistry.SetCommandTransaction(command); return(command.ExecuteScalar()); }
private int ExecuteNonQueryInternal(DbCommand command) { SetCommandTimeout(command); DbTransactionRegistry.SetCommandTransaction(command); if (BeforeExecuteCommand != null) { var args = new CommandExecuteEventArgs(command); BeforeExecuteCommand(this, args); if (args.Cancel) { throw new OperationCanceledException("Command Execution Canceled"); } } return(command.ExecuteNonQuery()); }
private DbDataReader ExecuteReaderInternal(DbCommand command, CommandBehavior behavior) { SetCommandTimeout(command); DbTransactionRegistry.SetCommandTransaction(command); if (BeforeExecuteCommand != null) { var args = new CommandExecuteEventArgs(command); BeforeExecuteCommand(this, args); if (args.Cancel) { throw new OperationCanceledException("Command Execution Canceled"); } } return(command.ExecuteReader(behavior)); }