title: How to check is connection string valid?
body: <p>I have to save data and I have to test connection before to save it. How can I test that this connection string is valid for a particular connection?</p>
  
  <p>My code is like this:</p>
  
  <pre><code>static public bool TestConnString(string connectionString)
  {
      bool returnVal = true;
      using (SqlConnection conn = new SqlConnection(connectionString))
      {
          try
          {
              conn.Open();
  
              if (conn.State != ConnectionState.Open)
                  returnVal = false;
              else
                  returnVal = true;
          }
          catch (Exception ex)
          {
              returnVal = false;
          }
      }
  
      return returnVal;
  }
  </code></pre>
  
  <p>Connection string is:</p>
  
  <pre><code>Data Source=testSvr03\SQLEXPRESS;Initial Catalog=Test; Connection Timeout=600; Persist Security Info=True;User ID=Test; password=test 
  </code></pre>
  
  <p>If I give wrong data source in connection String then it never returns in this function after conn.open() .I put catch block but it is coming in it</p>
  
  <p>Can anyone Tell me what is solution?</p>