Sql connection with Using

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
 
M

Mark Rae [MVP]

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

Hans Kesting

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
 
D

Duy Lam

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top