Working with MYSQL in C#:
I have a collection of parameterized IDbCommands I would like to execute. The are a mixture of updates, inserts, and deletes, right now I am doing:
using (IDbConnection connection = Connecter.CreateConnection())
{
foreach(IDBCommand command in m_commands)
{
command.Connection = connection;
command.ExecuteNonQuery();
}
}
The commands make heavy use of parameters so I can’t just combine the command texts of each command together. The performance is pretty terrible, and I know there has to be a better way.
Using the MYSql Bulk update took would be acceptable but it isn’t clear to me how to translate the parameter values into the file without risking SQL injection attacks.
Anyone have any suggestions?
(I have tied putting all the commands inside of a transaction which helps some but not enough)
If multiple inserts/updates/deletes are to be applied to the same table(s), consider using a MySqlDataAdapter/DataTable combination to batch together like statements. Setting the MySqlDataAdapter’s UpdateBatchSize property will enable/disable batch processing support. Thus, inserts/updates/deletes on the same table can be sent to the DB in a single trip.
I have done this will the OracleDataAdapter and SqlDataAdapter with significant performance gains. MySqlDataAdapter appears to implement basic batching as well (haven’t actually tested it though).
One suggestion would be to create your own wrapper class that hides the underlying DbDataAdapter/DataTable classes in some form of DbCommandBatch class. This class can then build up the required DataTable backing from an IDbCommand template and ultimately group together like commands if desired.
So the Create method could actually build up the required DataTable from a template command if desired (may make life easier depending on what code you already have) by iterating over the existing parameter collection. This could then create a class that implements an interface something like:
If this is on the right track for what you need I can provide sample code. If each command is distinct from one another (i.e., always different tables etc), then this will provide no value.
Note: In a perfect world, classes like SqlCommandSet etc would be publically available to avoid having to do this wonkiness.