Print Page | Close Window

Check DB connection

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=21525
Printed Date: 29 March 2026 at 7:41am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Check DB connection
Posted By: theSCIENTIST
Subject: Check DB connection
Date Posted: 09 October 2006 at 4:36am
I would like to know, how do I check if dbCon.Open is open so I don't re-open it, and if dbCon.Close is realy closed otherwise I have to close it.
 
I know there's a method, but can't find it.


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com



Replies:
Posted By: michael
Date Posted: 09 October 2006 at 2:14pm
If con.State = Data.ConnectionState.Open Then
con.Close()
End If

This is logically correct but if you use use connections right you should never need it.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: theSCIENTIST
Date Posted: 14 October 2006 at 8:31am
Thanks Michael it works fine, however I notived the .Dispose() method, is it safe to use this:
 
            dbCon.Open()
            dbCmd.ExecuteNonQuery()
            dbCmd.Dispose()
            dbCon.Close()
            dbCon.Dispose()
 
I'm disposing after I'm done with it, dbCon and dbCmd are objects that I then dispose of, in the case of dbCon I am closing the connection, is it beneficial to also dispose of it?


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: Mart
Date Posted: 14 October 2006 at 2:35pm
99% of the time it's beneficial to dispose of any object once you're certain you have finished with it. A closed connection can still be occupying resources, a connection that has been closed and disposed is no longer open and no longer occupies resources.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net