Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I multiple insert multiple records?

I have a class named Entry declared like this:

class Entry{     string Id {get;set;}     string Name {get;set;} }   

and then a method that will accept multiple such Entry objects for insertion into the database using ADO.NET:

static void InsertEntries(IEnumerable<Entry> 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         ...     } }   

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 Entry like this:

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();         }     }  }   

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?

like image 874
bottlenecked Avatar asked Jun 04 '10 09:06

bottlenecked


People also ask

How do I insert multiple rows of data?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

How do I insert multiple records in a table?

Insertion in a table is a DML (Data manipulation language) operation in SQL. When we want to store data we need to insert the data into the database. We use the INSERT statement to insert the data into the database.

Is it possible to insert multiple rows simultaneously?

To insert multiple rows, select the same number of rows that you want to insert. To select multiple rows hold down the "shift" key on your keyboard on a Mac or PC. For example, if you want to insert six rows, select six rows while holding the "shift" key.

How do I insert multiple records in MySQL?

MySQL INSERT multiple rows statement In this syntax: First, specify the name of table that you want to insert after the INSERT INTO keywords. Second, specify a comma-separated column list inside parentheses after the table name. Third, specify a comma-separated list of row data in the VALUES clause.


1 Answers

static void InsertSettings(IEnumerable<Entry> settings) {     using (SqlConnection oConnection = new SqlConnection("Data Source=(local);Initial Catalog=Wip;Integrated Security=True")) {         oConnection.Open();         using (SqlTransaction oTransaction = oConnection.BeginTransaction()) {             using (SqlCommand oCommand = oConnection.CreateCommand()) {                 oCommand.Transaction = oTransaction;                 oCommand.CommandType = CommandType.Text;                 oCommand.CommandText = "INSERT INTO [Setting] ([Key], [Value]) VALUES (@key, @value);";                 oCommand.Parameters.Add(new SqlParameter("@key", SqlDbType.NChar));                 oCommand.Parameters.Add(new SqlParameter("@value", SqlDbType.NChar));                 try {                     foreach (var oSetting in settings) {                         oCommand.Parameters[0].Value = oSetting.Key;                         oCommand.Parameters[1].Value = oSetting.Value;                         if (oCommand.ExecuteNonQuery() != 1) {                             //'handled as needed,                              //' but this snippet will throw an exception to force a rollback                             throw new InvalidProgramException();                         }                     }                     oTransaction.Commit();                 } catch (Exception) {                     oTransaction.Rollback();                     throw;                 }             }         }     } } 
like image 74
AMissico Avatar answered Sep 19 '22 10:09

AMissico