title: Database connection simplified - Is this right?
body: <p>I'm trying to teach myself c#, and have found various examples on connecting to a MSSQL database.  What I've done seems to be the simplest way to do it, but still seems overly complicated.</p>
  
  <p>Is there a better way?</p>
  
  <p>here's my code:</p>
  
  <pre><code>static void dbcon()
  {
      List&lt;int&gt; familyID = new List&lt;int&gt;();
      String connString = "Server=[myServer]\\[myInstance];Database=[dbName];User Id=[userID};Password=[password];";
      using (var sqlconn = new SqlConnection(connString))
      {
          using (var cmd = sqlconn.CreateCommand())
          {
              try
              {
                  sqlconn.Open();
                  cmd.CommandText = "SELECT id FROM family";
                  using (var reader = cmd.ExecuteReader())
                  {
  
                      while (reader.Read())
                      {
                          familyID.Add(Convert.ToInt32(reader["id"].ToString()));
                      }
                  }
                  foreach (int tempy in familyID)
                  {
                      Console.WriteLine("id: " + tempy);
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex);
              }                   
          }
      }
  }
  </code></pre>