title: Should I open and close db for each query?
body: <p>I am using old school ADO.net with C# so there is a lot of this kind of code. Is it better to make one function per query and open and close db each time, or run multiple queries with the same connection obect? Below is just one query for example purpose only.</p>
  
  <pre><code> using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectMain"].ConnectionString))
      {
          // Add user to database, so they can't vote multiple times
          string sql = " insert into PollRespondents (PollId, MemberId) values (@PollId, @MemberId)";
  
          SqlCommand sqlCmd = new SqlCommand(sql, connection);
  
          sqlCmd.Parameters.Add("@PollId", SqlDbType.Int);
          sqlCmd.Parameters["@PollId"].Value = PollId;
  
          sqlCmd.Parameters.Add("@MemberId", SqlDbType.Int);
          sqlCmd.Parameters["@MemberId"].Value = Session["MemberId"];
  
          try
          {
              connection.Open();
              Int32 rowsAffected = (int)sqlCmd.ExecuteNonQuery();
          }
          catch (Exception ex)
          {
              //Console.WriteLine(ex.Message);
          }
      }
  </code></pre>