Sql connection with Using

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

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
 
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...
 
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
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

The "Using" should close the connection (that's the function of Using)

Hans Kesting
 
David said:
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


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
 
Back
Top