/// <summary> /// Create and prepare a SqlCeCommand, 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 SqlCeConnection, on which to execute this command</param> /// <param name="transaction">A valid SqlCeTransaction, 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 SqlCeParameters 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 SqlCeHelper</param> /// <returns>SqlCeDataReader containing the results of the command</returns> private static SqlCeDataReader ExecuteReader(SqlCeConnection connection, SqlCeTransaction transaction, CommandType commandType, string commandText, SqlCeParameter[] commandParameters, SqlCeConnectionOwnership connectionOwnership) { if (connection == null) { throw new ArgumentNullException("connection"); } bool mustCloseConnection = false; // Create a command and prepare it for execution SqlCeCommand cmd = new SqlCeCommand(); try { PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection); // Create a reader SqlCeDataReader dataReader; // Call ExecuteReader with the appropriate CommandBehavior if (connectionOwnership == SqlCeConnectionOwnership.External) { dataReader = cmd.ExecuteReader(); } else { dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } // Detach the SqlCeParameters 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 SqlCeReader cant set its values. // When this happen, the parameters cant be used again in other command. bool canClear = true; foreach (SqlCeParameter commandParameter in cmd.Parameters) { if (commandParameter.Direction != ParameterDirection.Input) { canClear = false; } } if (canClear) { cmd.Parameters.Clear(); } return(dataReader); } catch { if (mustCloseConnection) { connection.Close(); } throw; } }
/// <summary> /// Create and prepare a SqlCeCommand, 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 SqlCeConnection, on which to execute this command</param> /// <param name="transaction">A valid SqlCeTransaction, or 'null'</param> /// <param name="commandType">The CommandType (TableDirect, Text)</param> /// <param name="commandText">The T-SQL command</param> /// <param name="commandParameters">An array of SqlCeParameters 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>SqlCeDataReader containing the results of the command</returns> private static SqlCeDataReader ExecuteReader(SqlCeConnection connection, SqlCeTransaction transaction, CommandType commandType, string commandText, SqlCeParameter[] commandParameters, SqlCeConnectionOwnership connectionOwnership) { if( connection == null ) throw new ArgumentNullException( "connection" ); bool mustCloseConnection = false; // Create a command and prepare it for execution SqlCeCommand cmd = new SqlCeCommand(); try { PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection ); // Create a reader SqlCeDataReader dataReader; // Call ExecuteReader with the appropriate CommandBehavior if (connectionOwnership == SqlCeConnectionOwnership.External) { dataReader = cmd.ExecuteReader(); } else { dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } // Detach the SqlCeParameters 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 can´t set its values. // When this happen, the parameters can´t be used again in other command. bool canClear = true; foreach(SqlCeParameter commandParameter in cmd.Parameters) { if (commandParameter.Direction != ParameterDirection.Input) canClear = false; } if (canClear) { cmd.Parameters.Clear(); } return dataReader; } catch { if( mustCloseConnection ) connection.Close(); throw; } }