473,396 Members | 1,738 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,396 software developers and data experts.

using statement

I have a using statement something like the following

try
{
using(SqlConnection cnn = ...)
{
}
catch(Exception e1)
{
}

What happens to the sqlconnection, if the sql statment inside the
using statement fails? Will it close the sql connection since it is
inside the using?
Thanks,
Sep 17 '08 #1
3 1669
CSharper <cs******@gmx.comwrote:
try
{
using(SqlConnection cnn = ...)
{
}
catch(Exception e1)
{
}

What happens to the sqlconnection, if the sql statment inside the
using statement fails? Will it close the sql connection since it is
inside the using?
Yes.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 17 '08 #2
CSharper brought next idea :
I have a using statement something like the following

try
{
using(SqlConnection cnn = ...)
{
}
catch(Exception e1)
{
}

What happens to the sqlconnection, if the sql statment inside the
using statement fails? Will it close the sql connection since it is
inside the using?
Thanks,
Yes, the using statement is compiled similarly to

SqlConnection cnn = null;
try
{
cnn = ...;
// the "using block"
}
finally
{
if (cnn != null)
cnn.Dispose(); // which will close the connection
}

this does not "catch" any exceptions, so your own "catch" block can
handle them (but the connection is closed before your "catch" block is
activated).

Hans Kesting
Sep 17 '08 #3
On Sep 17, 10:34*am, Hans Kesting <news.han...@spamgourmet.comwrote:
CSharper brought next idea :
I have a using statement something like the following
try
{
* *using(SqlConnection cnn = ...)
* *{
* *}
catch(Exception e1)
{
}
What happens to the sqlconnection, if the sql statment inside the
using statement fails? Will it close the sql connection since it is
inside the using?
Thanks,

Yes, the using statement is compiled similarly to

*SqlConnection cnn = null;
*try
*{
* *cnn = ...;
* *// the "using block"
* }
* finally
* {
* * if (cnn != null)
* * * cnn.Dispose(); // which will close the connection
* }

this does not "catch" any exceptions, so your own "catch" block can
handle them (but the connection is closed before your "catch" block is
activated).

Hans Kesting
Thanks both.
Sep 17 '08 #4

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

Similar topics

4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
5
by: Bill Priess | last post by:
Hey gang, Ok, I'm stumped on this one... I am using the using statement to wrap a SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope...
5
by: charliewest | last post by:
I've implemented the USING statement to ensure that my newly created sql connection closes when my method is finished using it. The USING statement is wrapped in try/catch error handling statement....
8
by: Andrew Robinson | last post by:
Are these two equivalent? Is one better than the other? I tend to go with #1 but started wondering.... Thanks, 1: using (SqlConnection cn = new SqlConnection(DataConnection)) using...
2
by: Robert Bravery | last post by:
Hi all, Being new to C# and .net I often don't know how to use things. I have created an app that imports excel data, it works well, with methods to open excel, extract the data and close excel....
6
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I am running a simple query using VB (This isnt a VB Question, dont worry!) on SQL Server Compact. I have the query below being created, and then added to if a flower location doesn't...
4
by: Jure Bogataj | last post by:
Hello! I'm using .NET 2005 (C#) and I've come across using statement. It seems ok, I was just wondering one thing. What if I returned object inside using statement using RETURN directive. Is...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
15
by: tshad | last post by:
Do I still need to close an object in a finally statement after a catch if it is part of a using statement? For example: FileStream fs = null; try { using (FileInfo fInfo = new...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.