One way to find out if a database connection really exists is to try to perform some operation on the connection. If the connection fails, the ConnectionState property is still "open", but when you try to do something with it, you will get your exception. For example:
SqlConnection sqlConn; private bool dbConnectionExists() { try { sqlConn.ChangeDatabase("MyDBname"); return true; } catch { return false; } } private void button1_Click(object sender, EventArgs e) { if (dbConnectionExists()) {
The connectionState property changes to "Closed" after this type of operation is completed and fails, so you can also check the status if you want.
Hope this helps.
Hades
source share