Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Sql connection with Using

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David C - 11 Dec 2007 15:00 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
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.