Functional Equivalence?

Tech-Archive recommends: Speed Up your PC by fixing your registry



I wanted to know if the using statement is functionally the same as
try{}finally{} when applied to ADO.Net. So, are these two following
code samples functionally equivalent?

public void SaveMessageData(string strTransaction)
{
SqlCommand comCommand = new SqlCommand(strTransaction,
new SqlConnection(ConnectionString));

try
{
comCommand.Connection.Open();
comCommand.ExecuteNonQuery();
}
finally
{
if(comCommand.Connection.State != ConnectionState.Closed)
{
comCommand.Connection.Close();
}
}

}

AND...

public void SaveMessageData(string strTransaction)
{
SqlCommand comCommand = new SqlCommand(strTransaction,
new SqlConnection(ConnectionString));

using(comCommand)
{
comCommand.Connection.Open();
comCommand.ExecuteNonQuery();
}
}

I ask this because I saw some example code from Microsoft that uses the
'using' statement in a file access scenario. The author does not
explicitly close the file after writing to it but the code works fine.

Note that I do not use the catch() portion of the TryCatchFinally
because I want all exceptions from my DAL to bubble up.


Cheers!
Russ

.