title: Insert A list of objects into SQL Server table
body: <p>I want to insert a list of objects into sql server table. However, currently, I have to open and close the sql connection each time I insert a record row. </p>
  
  <p>I just wonder if there is a way I can insert all the objects in the record list at one time? Here is the code snippet.</p>
  
  <pre><code>public void InsertDataToDb()
  {
      string connectionString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
      var records = GetRecords();
  
      using (SqlConnection conn = new SqlConnection(connectionString))
      {
          SqlCommand cmd =
              new SqlCommand(
                  "INSERT INTO TableName (param1, param2, param3) VALUES (@param1, @param2, @param3)");
          cmd.CommandType = CommandType.Text;
          cmd.Connection = conn;
          foreach (var item in records)
          {
              cmd.Parameters.AddWithValue("@param1", item.param1);
              cmd.Parameters.AddWithValue("@param2", item.param2);
              cmd.Parameters.AddWithValue("@param3", item.param3);
  
              conn.Open();
              cmd.ExecuteNonQuery();
              cmd.Parameters.Clear();
              conn.Close();
          }
      }
  }
  </code></pre>