I have a class function that accesses an SQL database with a "Using"
process. Do I need to close the connection specifically, or will the
try...catch process close it when it exits the Using? Below is the Using
sample. Thanks.
David
Using conn As New
SqlConnection(ConfigurationManager.ConnectionStrings("RFPDataConnectionString").ConnectionString)
...
Try
...
Catch
End Using
Mark Rae [MVP] - 11 Dec 2007 15:36 GMT
> Do I need to close the connection specifically,
No.
> will the try...catch process close it when it exits the Using?
The try...catch won't close it per se - the Using will close it...

Signature
Mark Rae
ASP.NET MVP
http://www.markrae.net
Hans Kesting - 11 Dec 2007 15:42 GMT
David C formulated the question :
> I have a class function that accesses an SQL database with a "Using" process.
> Do I need to close the connection specifically, or will the try...catch
[quoted text clipped - 13 lines]
>
> End Using
The "Using" should close the connection (that's the function of Using)
Hans Kesting
Duy Lam - 11 Dec 2007 16:06 GMT
> I have a class function that accesses an SQL database with a "Using"
> process. Do I need to close the connection specifically, or will the
[quoted text clipped - 13 lines]
>
> End Using
Let test sample code :
using (MyClass m = new MyClass())
{
throw new ApplicationException("something wrong");
}
class MyClass : IDisposable
{
#region IDisposable Members
void IDisposable.Dispose()
{
Console.WriteLine("hey, i'm destroyed");
}
#endregion
}
Running using block and what you see :-)
And SqlConnection class already implemented IDisposable

Signature
Duy Lam Phuong