Functional Equivalence?
- From: "Dinsdale" <russ.haley@xxxxxxxxx>
- Date: 30 May 2006 15:07:19 -0700
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
.
- Follow-Ups:
- Re: Functional Equivalence?
- From: John B
- Re: Functional Equivalence?
- From: sdbillsfan
- Re: Functional Equivalence?
- From: Mehdi
- Re: Functional Equivalence?
- Prev by Date: Windows Authentication
- Next by Date: Re: Complex if statement.
- Previous by thread: Windows Authentication
- Next by thread: Re: Functional Equivalence?
- Index(es):