Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

[.net] Clarification on Using statement for vb.net/asp.net

This topic is 3336 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

is this:
Dim sqlConn1 As SqlConnection = New SqlConnection("Server=aServer;Database=Web;User ID=aPerson;Password=aPWD")
        sqlConn1.Open()
        ' .... other code .....
        sqlConn1.Close()
        sqlConn1.Dispose()
The same as this:
using (sqlConn1 As SqlConnection = New SqlConnection("Server=aServer;Database=Web;User ID=aPerson;Password=aPWD"))
        sqlConn1.Open()
        ' .... other code .....
end using
Really I want to know if the using statement will Close and Dispose of my SqlConnection (or any other Sql-related .NET structure).

Share this post


Link to post
Share on other sites
Advertisement
using will just call Dispose().

The IDisposable object you are working with may call Close() as part of its own Dispose() implementation.

Share this post


Link to post
Share on other sites
To be more precise:

Using con As SqlConnection = New SqlConnection("Server=aServer;Database=Web;User ID=aPerson;Password=aPWD")
con.Open()
End Using


is more or less the same as

Dim con As SqlConnection = New SqlConnection("Server=aServer;Database=Web;User ID=aPerson;Password=aPWD")
Try
con.Open()
Finally
con.Dispose()
End Try


Using therefore guarantees that Dispose is called if the connection instance was created even if an exception is thrown within the Using body.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!