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

using keyword

I've recently started using the using keyword for my connections, but I
have just seen some code where you have a using within a using, which is
opening a command :

using (SqlConnection _conn = new
SqlConnection(ConfigurationManager.ConnectionStrin gs["constreng"].ToStri
ng()))
{
using (SqlCommand _comm = new
SqlCommand("GetRevenueByOwner", _conn))
{

Is it good practice to use something like this?

*** Sent via Developersdex http://www.developersdex.com ***
Jul 12 '07 #1
1 3233
Mike,

Yes, it is, since you are disposing of the command when you are done
with it. There is nothing wrong with nested using statements.

The only thing you have to worry about is if an object in a nested
statement uses an object in the outer statement, and you want to use that
outer object after the inner object is done. It is possible that the inner
object will call dispose in it's implementation of Dispose.

In your example:

using (SqlConnection _conn =
new
SqlConnection(ConfigurationManager.ConnectionStrin gs["constreng"].ToString()))
{
using (SqlCommand _comm = new SqlCommand("GetRevenueByOwner", _conn))
{

}

// Do something with _conn here
}

You don't have a problem with using _conn, since the command doesn't
dispose of the connection when Dispose is called on it. However, for
something like a TextReader attached to a stream, you might run into this.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike P" <mi*******@gmail.comwrote in message
news:OR**************@TK2MSFTNGP05.phx.gbl...
I've recently started using the using keyword for my connections, but I
have just seen some code where you have a using within a using, which is
opening a command :

using (SqlConnection _conn = new
SqlConnection(ConfigurationManager.ConnectionStrin gs["constreng"].ToStri
ng()))
{
using (SqlCommand _comm = new
SqlCommand("GetRevenueByOwner", _conn))
{

Is it good practice to use something like this?

*** Sent via Developersdex http://www.developersdex.com ***

Jul 12 '07 #2

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

Similar topics

0
by: Robert Brewer | last post by:
Paul Moore wrote: > ...I *can* defend an argument that the proposal must > take a stance on the keyword proposed. The existence of people (me, > as an example :-)) whose vote would be changed by...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
1
by: Mehul Patel | last post by:
Our .Net team have been pondering about using keyword. We are using streams FileStream and BufferedStream. We use using keyword at FileStream, and not BufferedStream which wraps FileStream. So...
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
3
by: Brian Gideon | last post by:
I stumbled across something odd today about the placement of the using keyword. Section 9.3.2 of the C# v1.1 specification did not answer my question. My confusion is isolated to what happens in...
3
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an...
11
by: youngster94 | last post by:
Hey all, I've written a VB.Net app that creates picture badges complete with barcodes. The problem is that the barcode quality is not good enough to be read by scanners. I'm using the...
3
by: Igor | last post by:
Is there possibility to use IF conditions inside SELECT statements? For example, can i write something like this: CREATE PROCEDURE ( @OPTION int, @KEYWORD nvarchar(40) ) AS BEGIN
4
by: Pranjal9880 | last post by:
Hi all I am trying to parse the xml file using perl in which I am succeeded , I am able to fetch the data from the xml file by using one keyword. Now I want to do it using more than one keyword. It...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.