c# - Is there a standard .NET way to test if a SqlConnection string works? -


possible duplicate:
how check if connection string valid?

currently i'm doing this:

internal bool checkconnection() {     using (sqlconnection testconn = new sqlconnection(this.connectionstring))     {         try         {             testconn.open();         }         catch (sqlexception)         {             return false;         }     }     return true; } 

is there better way this?

that pretty way it. though should think handling other exception types well. there can other reasons why don't connect db server besides sql issue.

connection.open can throw invalidoperationexception , argumentexception in addition sqlexception. apis .open calls can throw other types of exceptions can percolate code well. in fact, 1 of rare instances when might preferable handle base exception , display message user. (the general rule of thumb handle specific exceptions.)


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -