private static IDataReader ExecuteReader(IDbConnection connection, IDbTransaction transaction, CommandType commandType, String commandText, IDbDataParameter[] commandParameters, ConnectionOwnership connectionOwnership) { IDataReader reader2; if (connection == null) { throw new ArgumentNullException("connection"); } bool mustCloseConnection = false; IDbCommand command = null; switch (ProviderType) { case Util.ConnectionLibrary.SQlClient: command = new SqlCommand(); break; case Util.ConnectionLibrary.Oledb: command = new OleDbCommand(); break; case Util.ConnectionLibrary.ODBC: command = new OdbcCommand(); break; default: command = null; break; } try { IDataReader reader; PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection); if (connectionOwnership == ConnectionOwnership.External) { reader = command.ExecuteReader(); } else { reader = command.ExecuteReader(CommandBehavior.CloseConnection); } bool flag2 = true; foreach (IDbDataParameter parameter in command.Parameters) { if (parameter.Direction != ParameterDirection.Input) { flag2 = false; } } if (flag2) { command.Parameters.Clear(); } reader2 = reader; } catch { if (mustCloseConnection) { connection.Close(); } throw; } return reader2; }
/// <summary> /// Create and prepare a IDbCommand, 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 IDbConnection , on which to execute this command</param> /// <param name="transaction">a valid IDbTransaction , 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 IDbDataParameter s 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 SqlHelper</param> /// <returns>DataReader containing the results of the command</returns> private static SQLiteDataReader ExecuteReader(IDbConnection connection, IDbTransaction transaction, CommandType commandType, string commandText, IDbDataParameter[] commandParameters, ConnectionOwnership connectionOwnership) { //create a command and prepare it for execution SQLiteCommand cmd = new SQLiteCommand(); PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters); //create a reader SQLiteDataReader dr; // call ExecuteReader with the appropriate CommandBehavior if (connectionOwnership == ConnectionOwnership.External) { dr = cmd.ExecuteReader(); } else { dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); } // detach the IDbDataParameter s from the command object, so they can be used again. cmd.Parameters.Clear(); return(dr); }