Just adding all CommandTexts to one large command command is not as useful as it seems.
The main advantage of prepared statements in C # is that the workload in the database is executed when the team is created. No, when you execute it (for example, with ExecuteNonQuery() -, which executes a command only if you do not have a transaction object].
To avoid this and create a workload in the database only once for all your operators, it is much better to create a Transaction object and complete the transaction. Then all the commands will be executed without additional load in the database.
This would be a better application:
// Try to create the Command as early as possible with a valid Connection object string commandString = "UPDATE Mytable SET s_id=@s _id where id = @id;"; var command = new SqlCommand(commandString, connection); // Then define a Transaction object with your Connection var transaction = connection.BeginTransaction(); command.Transaction = transaction; // Now iterate through your array for(int i=0; i<array.Length; i++) { command.Parameters.Add("@s_id", SqlDbType.YourType).Value = items[i].SId; command.Parameters.Add("@id", SqlDbType.YourType).Value = items[i].Id; command.ExecuteNonQuery(); // Not executed at this point } // And now execute it with the possibility to rollback all commands when it fails try { transaction.Commit(); } // Here the execution is committed to the DB catch (Exception) { transaction.Rollback(); throw; }
FluepkeSchaeng
source share