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(C onfigurationMan ager.Connection Strings["constreng"].ToString()))
{
using (SqlCommand _comm = new SqlCommand("Get RevenueByOwner" , _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.co m
"Mike P" <mi*******@gmai l.comwrote in message
news:OR******** ******@TK2MSFTN GP05.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(C onfigurationMan ager.Connection Strings["constreng"].ToStri
ng()))
{
using (SqlCommand _comm = new
SqlCommand("Get RevenueByOwner" , _conn))
{
Is it good practice to use something like this?
*** Sent via Developersdex http://www.developersdex.com ***