Knowing if connection failed to sql server in C#
-
I'm not good at SQL indulgence. Am not sure if it's a theme for database or C #. I want to be able to distinguish when logging into my SQL database if the person has used the correct user name. As usual connection to SQL Server looks like this in C #. Using SQL connection and not the Windows connection. SqlConnection con = new SqlConnection ("connection string with the user name here"); con.Open (); Question: How can I know if the connection with the user name for the SQL server has failed using C # code.
-
I'm not good at SQL indulgence. Am not sure if it's a theme for database or C #. I want to be able to distinguish when logging into my SQL database if the person has used the correct user name. As usual connection to SQL Server looks like this in C #. Using SQL connection and not the Windows connection. SqlConnection con = new SqlConnection ("connection string with the user name here"); con.Open (); Question: How can I know if the connection with the user name for the SQL server has failed using C # code.
SqlConnection con = new SqlConnection("connection string with the user name here"); try { con.Open(); } catch (SqlException ex) { //Handle exception here }
Wrong is evil and must be defeated. - Jeff Ello
-
SqlConnection con = new SqlConnection("connection string with the user name here"); try { con.Open(); } catch (SqlException ex) { //Handle exception here }
Wrong is evil and must be defeated. - Jeff Ello
Jörgen Andersson wrote:
catch (Exception ex)
You should only be catching exception types which you can handle. In this case, it should be sufficient to catch
SqlException
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Jörgen Andersson wrote:
catch (Exception ex)
You should only be catching exception types which you can handle. In this case, it should be sufficient to catch
SqlException
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Yes, you're right, I've updated the answer.
Wrong is evil and must be defeated. - Jeff Ello
-
SqlConnection con = new SqlConnection("connection string with the user name here"); try { con.Open(); } catch (SqlException ex) { //Handle exception here }
Wrong is evil and must be defeated. - Jeff Ello