title: How should I multiple insert multiple records?
body: <p>I have a class named <code>Entry</code> declared like this:  </p>
  
  <pre><code>class Entry{
      string Id {get;set;}
      string Name {get;set;}
  }  
  </code></pre>
  
  <p>and then a method that will accept multiple such <code>Entry</code> objects for insertion into the database using ADO.NET:  </p>
  
  <pre><code>static void InsertEntries(IEnumerable&lt;Entry&gt; entries){
      //build a SqlCommand object
      using(SqlCommand cmd = new SqlCommand()){
          ...
          const string refcmdText = "INSERT INTO Entries (id, name) VALUES (@id{0},@name{0});";
          int count = 0;
          string query = string.Empty;
          //build a large query
          foreach(var entry in entries){
              query += string.Format(refcmdText, count);
              cmd.Parameters.AddWithValue(string.Format("@id{0}",count), entry.Id);
              cmd.Parameters.AddWithValue(string.Format("@name{0}",count), entry.Name);
              count++;
          }
          cmd.CommandText=query;
          //and then execute the command
          ...
      }
  }  
  </code></pre>
  
  <p>And my question is this: should I keep using the above way of sending multiple insert statements (build a giant string of insert statements and their parameters and send it over the network), or should I keep an open connection and send a single insert statement for each <code>Entry</code> like this:  </p>
  
  <pre><code>using(SqlCommand cmd = new SqlCommand(){
      using(SqlConnection conn = new SqlConnection(){
          //assign connection string and open connection
          ...
          cmd.Connection = conn;
          foreach(var entry in entries){
              cmd.CommandText= "INSERT INTO Entries (id, name) VALUES (@id,@name);";
              cmd.Parameters.AddWithValue("@id", entry.Id);
              cmd.Parameters.AddWithValue("@name", entry.Name);
              cmd.ExecuteNonQuery();
          }
      }
   }  
  </code></pre>
  
  <p>What do you think? Will there be a performance difference in the Sql Server between the two? Are there any other consequences I should be aware of?</p>