using ( SqlConnection connName1 = new SqlConnection ( "....... <database1>
{
..............
using ( SqlConnection connName2 = new SqlConnection ( "....... <database2>
{
.............
}
}
Can I nest using statement blocks
Thanks.

are using statements nestable?
hmds
yes you can! :-)
try it and see what happens. It's the best way to learn. you can have multiple using statements nested in each other but remember that any objects created within the using blocks will only be available in that scope unless you declared it outside the scope and using the objects in the using blocks
example:
using (....)
{
using (...)
{
string test = "test"; //this can only be used in this 2nd using statement only
}
}
if it were:
string test = String.Empty;
using (....)
{
using (...)
{
test = "test"; //this can only be accessed here and outside this using block, since you instantiated/declared it outside the using blocks
}
}
AoggY
PPR
Hell YES
,
Best Regards,
Rizwan
redneon
advantage of using blocks Well its best practice since they will dispose of the disposable object when its finished using it instead of still hogging up memory perhaps in your application.
this is really one of many reasons perhaps why it is an advantage of using the using blocks. you also know what objects you are dealing with.
http://msdn.microsoft.com/library/default.asp url=/library/en-us/csref/html/vclrfusingstatement.asp
http://msdn2.microsoft.com/en-us/library/yh598w02(VS.80).aspx
It's best to release the memory usage whenever possible to maintain performance at times but mostly to free up memory. the CLR (Common Language Runtime) will run the GC (Garbage Collector) whenever it feels it needs to however the using block as stated in the docs will release memory when it finishes using that object.
The object should implement the IDisposible interface to indicate it is a disposable object
RAYMOND KNIGHT
alastairf
using ( SqlConnection connName1 = new SqlConnection ( "....... <database1>"))
using ( SqlConnection connName2 = new SqlConnection ( "....... <database2>"))
{
//block of code for using two objects
}