tags:

views:

274

answers:

2

I am just browsing the code in the SQLHelper Class V2 and I notice the following

 public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 {
  if( transaction == null ) throw new ArgumentNullException( "transaction" );
  if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );

  // Create a command and prepare it for execution
  SqlCommand cmd = new SqlCommand();
  bool mustCloseConnection = false;
  PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );

  // Finally, execute the command
  int retval = cmd.ExecuteNonQuery();

  // Detach the SqlParameters from the command object, so they can be used again
  cmd.Parameters.Clear();
  return retval;
 }

Is there a reason why the command wasn't within a "Using Bolck"? I see the use of the"Using Block" elsewhere in the code.

A: 

My guess is that its because the command is used throughout the scope of the function, so it would just add extra code. Different developer preferring different implementations.

Chris Klepeis
A: 

What does PrepareCommand do?

shahkalpesh