title: How to check successful connection to a SQL Server database in C#?
body: <p>I am trying to connect to the SQL Server database in C#, and check that the database contains an empty table with 3 columns, but I don't know how to check if it is successful or not..</p>
  
  <p>My code:</p>
  
  <pre><code>protected bool checkDB()
  { 
      string ConnectionString = "Server=[serverName];Database=[databaseName];Trusted_Connection=true";
  
      SqlConnection con = new SqlConnection(ConnectionString);
  
      SqlCommand com = new SqlCommand("SELECT * FROM tableName", con);
  
      // use the connection here
      con.Open();
  
      con.Close();
  
      if (success)
      {
          return true;
      }
      else
      {
          return false;
      }
  }
  </code></pre>