473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Functional Equivalence?

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

May 30 '06 #1
5 1229
On 30 May 2006 15:07:19 -0700, Dinsdale wrote:
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?


When you are using the using statement, the compiler transforms it into a
try/finally statement and calls Dispose() in the finally block. So you 2
code sample are not exactly equivalent. I'm not sure if the SqlCommand's
Dispose() method closes its connection. A better way to write it would be:

public void SaveMessageData(string strTransaction)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand comCommand = new SqlCommand(strTransaction,
conn);
comCommand.Connection.Open();
comCommand.ExecuteNonQuery();
}
}

With the above code, the using statement ensures that the connection will
be closed no matter what.
May 30 '06 #2
When you have questions like this you should take a look "under the
hood" using the ildasm tool.
http://msdn2.microsoft.com/en-us/library/f7dy01k1.aspx

Here's the IL from your function with the "using" statement, see it
translates to a try/finally format and calls IDisposable.Dispose() as
the other poster indicated.

.maxstack 3
.locals init ([0] class [System.Data]System.Data.SqlClient.SqlCommand
comCommand,
[1] class [System.Data]System.Data.SqlClient.SqlCommand
CS$00000005$00000000)
IL_0000: ldarg.1
IL_0001: ldarg.0
IL_0002: ldfld string test::ConnectionString
IL_0007: newobj instance void
[System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
IL_000c: newobj instance void
[System.Data]System.Data.SqlClient.SqlCommand::.ctor(string,

class
[System.Data]System.Data.SqlClient.SqlConnection)
IL_0011: stloc.0
IL_0012: ldloc.0
IL_0013: stloc.1
.try
{
IL_0014: ldloc.0
IL_0015: callvirt instance class
[System.Data]System.Data.SqlClient.SqlConnection
[System.Data]System.Data.SqlClient.SqlCommand::get_Connection()
IL_001a: callvirt instance void
[System.Data]System.Data.SqlClient.SqlConnection::Open()
IL_001f: ldloc.0
IL_0020: callvirt instance int32
[System.Data]System.Data.SqlClient.SqlCommand::ExecuteNonQuery( )
IL_0025: pop
IL_0026: leave.s IL_0032
} // end .try
finally
{
IL_0028: ldloc.1
IL_0029: brfalse.s IL_0031
IL_002b: ldloc.1
IL_002c: callvirt instance void
[mscorlib]System.IDisposable::Dispose()
IL_0031: endfinally
} // end handler
IL_0032: ret

May 30 '06 #3
Thanks for the quick response guys.

I wish you hadn't shown me that tool, I'm not going to get any work
done now... ;)

Russ

May 30 '06 #4
Dinsdale wrote:
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?

I think equivalent would be:

....
try
{
...
}
finally
{
comCommand.Dispose();
}

JB
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

May 31 '06 #5
I won't mention Reflector, then ;-p
Quite simply invaluable for disecting (non-obfuscated) .Net code... and it's
friendlier that ILDASM (e.g. pick an assembly, then a class, then member,
then disassemble into any of IL, C#, VB, Delphi, MC++ or Chrome, or leap
directly into the MSDN, or the online help, or see what uses it, etc.
Invaluable.

http://www.aisto.com/roeder/dotnet/
May 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
10
by: Xah Lee | last post by:
another functional exercise with lists. Here's the perl documentation. I'll post a perl and the translated python version in 48 hours. =pod parti(aList, equalFunc) given a list aList of...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
1
by: Kenneth P | last post by:
Hi, I'm trying to do some Custom Paging technique with the datagrid object and with the select command in Sql, thus forcing the server to only select those rows from the database that should be...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
15
by: Lorenzo Stella | last post by:
Hi all, I haven't experienced functional programming very much, but now I'm trying to learn Haskell and I've learned that: 1) in functional programming LISTS are fundmental; 2) any "cycle" in FP...
15
by: jzakiya | last post by:
I'm translating a program in Python that has this IF Then chain IF x1 < limit: --- do a --- IF x2 < limit: --- do b --- IF x3 < limit: --- do c --- .----- ------ IF x10 < limt: ---...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.