/// <summary> /// The execute reader. /// </summary> /// <param name="connection"> /// The connection. /// </param> /// <param name="transaction"> /// The transaction. /// </param> /// <param name="commandType"> /// The command type. /// </param> /// <param name="commandText"> /// The command text. /// </param> /// <param name="commandParameters"> /// The command parameters. /// </param> /// <param name="connectionOwnership"> /// The connection ownership. /// </param> /// <returns> /// The <see cref="OleDbDataReader"/>. /// </returns> private static OleDbDataReader ExecuteReader( OleDbConnection connection, OleDbTransaction transaction, CommandType commandType, string commandText, OleDbParameter[] commandParameters, OleDbConnectionOwnership connectionOwnership) { // create a command and prepare it for execution var cmd = new OleDbCommand(); PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters); // create a reader OleDbDataReader dr; // call ExecuteReader with the appropriate CommandBehavior if (connectionOwnership == OleDbConnectionOwnership.External) { dr = cmd.ExecuteReader(); } else { dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); } return(dr); }
/// <summary> /// Create and prepare an OleDbCommand, and call ExecuteReader with the appropriate CommandBehavior. /// </summary> /// <remarks> /// If we created and opened the connection, we want the connection to be closed when the DataReader is closed. /// /// If the caller provided the connection, we want to leave it to them to manage. /// </remarks> /// <param name="connection">a valid OleDbConnection, on which to execute this command</param> /// <param name="transaction">a valid OleDbTransaction, or 'null'</param> /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> /// <param name="commandText">the stored procedure name or PL/SQL command</param> /// <param name="commandParameters">an array of OleDbParameters to be associated with the command or 'null' if no parameters are required</param> /// <param name="connectionOwnership">indicates whether the connection parameter was provided by the caller, or created by OleDbHelper</param> /// <returns>OleDbDataReader containing the results of the command</returns> private static OleDbDataReader ExecuteReader(OleDbConnection connection, OleDbTransaction transaction, CommandType commandType, string commandText, OleDbParameter[] commandParameters, OleDbConnectionOwnership connectionOwnership) { //create a command and prepare it for execution OleDbCommand cmd = new OleDbCommand(); PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters); //create a reader OleDbDataReader dr; // call ExecuteReader with the appropriate CommandBehavior if (connectionOwnership == OleDbConnectionOwnership.External) { dr = cmd.ExecuteReader(); } else { dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); } return dr; }
private static OleDbDataReader ExecuteReader(OleDbConnection connection, OleDbTransaction transaction, string commandText, OleDbParameter[] commandParameters, OleDbConnectionOwnership connectionOwnership) { if (null == connection) throw new ArgumentNullException("connection"); bool mustCloseConnection = false; OleDbCommand cmd = new OleDbCommand(); try { PrepareCommand(cmd, connection, transaction, commandText, commandParameters, out mustCloseConnection); OleDbDataReader dataReader; if (OleDbConnectionOwnership.External == connectionOwnership) { dataReader = cmd.ExecuteReader(); } else { dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } bool canClear = true; foreach (OleDbParameter commandParameter in cmd.Parameters) { if (ParameterDirection.Input != commandParameter.Direction) canClear = false; } if (canClear) { cmd.Parameters.Clear(); } return dataReader; } catch { if (mustCloseConnection) connection.Close(); throw; } }
/// <summary> /// Create and prepare a OleDbCommand, and call ExecuteReader with the appropriate CommandBehavior. /// </summary> /// <remarks> /// If we created and opened the connection, we want the connection to be closed when the DataReader is closed. /// /// If the caller provided the connection, we want to leave it to them to manage. /// </remarks> /// <param name="connection">A valid OleDbConnection, on which to execute this command</param> /// <param name="transaction">A valid OleDbTransaction, or 'null'</param> /// <param name="commandType">The CommandType (stored procedure, text, etc.)</param> /// <param name="commandText">The stored procedure name or T-SQL command</param> /// <param name="commandParameters">An array of OleDbParameters to be associated with the command or 'null' if no parameters are required</param> /// <param name="connectionOwnership">Indicates whether the connection parameter was provided by the caller, or created by OleDBHelper</param> /// <returns>OleDbDataReader containing the results of the command</returns> private static OleDbDataReader ExecuteReader(OleDbConnection connection, OleDbTransaction transaction, CommandType commandType, string commandText, OleDbParameter[] commandParameters, OleDbConnectionOwnership connectionOwnership) { if (connection == null) throw new ArgumentNullException("connection"); bool mustCloseConnection = false; // Create a command and prepare it for execution OleDbCommand cmd = new OleDbCommand(); try { PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection); // Create a reader OleDbDataReader dataReader; // Call ExecuteReader with the appropriate CommandBehavior if (connectionOwnership == OleDbConnectionOwnership.External) { dataReader = cmd.ExecuteReader(); } else { dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } // Detach the OleDbParameters from the command object, so they can be used again. // HACK: There is a problem here, the output parameter values are fletched // when the reader is closed, so if the parameters are detached from the command // then the SqlReader cant set its values. // When this happen, the parameters cant be used again in other command. bool canClear = true; foreach (OleDbParameter commandParameter in cmd.Parameters) { if (commandParameter.Direction != ParameterDirection.Input) canClear = false; } if (canClear) { cmd.Parameters.Clear(); } return dataReader; } catch { if (mustCloseConnection) connection.Close(); throw; } }
private static OleDbDataReader ExecuteReader(OleDbConnection connection, OleDbTransaction transaction, string commandText, OleDbParameter[] commandParameters, OleDbConnectionOwnership connectionOwnership) { if (null == connection) { throw new ArgumentNullException("connection"); } bool mustCloseConnection = false; OleDbCommand cmd = new OleDbCommand(); try { PrepareCommand(cmd, connection, transaction, commandText, commandParameters, out mustCloseConnection); OleDbDataReader dataReader; if (OleDbConnectionOwnership.External == connectionOwnership) { dataReader = cmd.ExecuteReader(); } else { dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } bool canClear = true; foreach (OleDbParameter commandParameter in cmd.Parameters) { if (ParameterDirection.Input != commandParameter.Direction) { canClear = false; } } if (canClear) { cmd.Parameters.Clear(); } return(dataReader); } catch { if (mustCloseConnection) { connection.Close(); } throw; } }