Batch Processing
The CData ADO.NET Provider for MySQL enables you to take advantage of the bulk load support in MySQL through MySQLDataAdapters. You can use the Batch API to execute related SQL data manipulation statements simultaneously.
Using the ADO.NET Batch API
Performing a batch update consists of the following basic steps:
- Define custom parameterized SQL statements in MySQLCommand objects.
- Set the UpdatedRowSource property of the MySQLCommand object to "UpdateRowSource.None".
- Assign the MySQLCommand objects to the MySQLDataAdapter.
- Add the parameters to the command.
- Call the MySQLDataAdapter's Update method. Pass in a DataSet or DataTable containing your changes.
Controlling Batch Size
Depending on factors such as the size of the request, your network resources, and the performance of the server, you may gain performance by executing several smaller batch requests. You can control the size of each batch by setting the MySQLDataAdapter's UpdateBatchSize property to a positive integer.
Bulk INSERT
The following code prepares a single batch that inserts records in bulk and retrieves the new records' Ids. The example executes a batch INSERT of new DataRows, which have the "Added" state.
C#
MySQLDataAdapter adapter = new MySQLDataAdapter(); using (MySQLConnection conn = new MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")) { conn.Open(); adapter.InsertCommand = conn.CreateCommand(); adapter.InsertCommand.CommandText = "INSERT INTO `sakila`.Orders (ShipCity) VALUES (@ShipCity)"; adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None; adapter.InsertCommand.Parameters.Add("@ShipCity", "ShipCity"); DataTable batchDataTable = new DataTable(); batchDataTable.Columns.Add("ShipCity", typeof(string)); batchDataTable.Rows.Add("Raleigh"); batchDataTable.Rows.Add("New York"); adapter.UpdateBatchSize = 2; adapter.Update(batchDataTable); MySQLCommand cmd = new MySQLCommand("SELECT * FROM LastResultInfo#TEMP", conn); adapter = new MySQLDataAdapter(cmd); DataTable res = new DataTable(); adapter.Fill(res); foreach (DataRow row in res.Rows) foreach(DataColumn col in res.Columns) Console.Write("{0}: {1}", col.ColumnName, row[col]); }
VB.NET
Dim adapter As New MySQLDataAdapter()
Using conn As New MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
conn.Open()
adapter.InsertCommand = conn.CreateCommand()
adapter.InsertCommand.CommandText = "INSERT INTO `sakila`.Orders (ShipName) VALUES (@ShipCity)"
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None
adapter.InsertCommand.Parameters.Add("@ShipCity", "ShipCity")
Dim batchDataTable As New DataTable()
batchDataTable.Columns.Add("ShipCity", GetType(String))
batchDataTable.Rows.Add("New York")
batchDataTable.Rows.Add("Raleigh")
adapter.UpdateBatchSize = 2
adapter.Update(batchDataTable)
Dim cmd As New MySQLCommand("SELECT * FROM LastResultInfo#TEMP", conn)
adapter = New MySQLDataAdapter(cmd)
Dim res As New DataTable()
adapter.Fill(res)
For Each row As DataRow In res.Rows
For Each col As DataColumn In res.Columns
Console.WriteLine("{0}: {1}", col.ColumnName, row(col))
Next
Next
End Using
Bulk Update
A batch update additionally requires the primary key of each row to update. The following example executes a batch for all DataRow records with a "Modified" state:
C#
MySQLDataAdapter adapter = new MySQLDataAdapter(); using (MySQLConnection conn = new MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")) { conn.Open(); adapter.UpdateCommand = conn.CreateCommand(); adapter.UpdateCommand.CommandText = "UPDATE `sakila`.Orders SET ShipCity=@ShipCity WHERE Id=@Id"; adapter.UpdateCommand.Parameters.Add("@ShipCity", "ShipCity"); adapter.UpdateCommand.Parameters.Add("@Id", "Id"); adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None; DataTable batchDataTable = new DataTable(); batchDataTable.Columns.Add("ShipCity", typeof(string)); batchDataTable.Rows.Add("Raleigh"); batchDataTable.Rows.Add("New York"); adapter.UpdateBatchSize = 2; adapter.Update(dataTable); }
VB.NET
Dim adapter As New MySQLDataAdapter()
Using conn As New MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
conn.Open()
adapter.UpdateCommand = conn.CreateCommand()
adapter.UpdateCommand.CommandText = "UPDATE `sakila`.Orders SET ShipCity=@ShipCity WHERE Id=@Id"
adapter.UpdateCommand.Parameters.Add("@ShipCity", "ShipCity")
adapter.UpdateCommand.Parameters.Add("@Id", "Id")
adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None
Dim batchDataTable As New DataTable()
batchDataTable.Columns.Add("ShipCity", GetType(String))
batchDataTable.Rows.Add("New York")
batchDataTable.Rows.Add("Raleigh")
adapter.UpdateBatchSize = 2
adapter.Update(dataTable)
End Using
Bulk Delete
The following code prepares a single batch that deletes records in bulk. The primary key for each row is required. The following example executes a batch for all DataRow records with a "Deleted" state:
C#
MySQLDataAdapter adapter = new MySQLDataAdapter();
using (MySQLConnection conn = new MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")) {
conn.Open();
adapter.DeleteCommand = conn.CreateCommand();
adapter.DeleteCommand.CommandText = "DELETE FROM `sakila`.Orders WHERE Id=@Id";
adapter.DeleteCommand.Parameters.Add("@Id", "Id");
adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
adapter.UpdateBatchSize = 2;
adpater.Update(table);
}
VB.NET
Dim adapter As New MySQLDataAdapter()
Using conn As New MySQLConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
conn.Open()
adapter.DeleteCommand = conn.CreateCommand()
adapter.DeleteCommand.CommandText = "DELETE FROM `sakila`.Orders WHERE Id=@Id"
adapter.DeleteCommand.Parameters.Add("@Id", "Id")
adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None
adapter.UpdateBatchSize = 2
adpater.Update(table)
End Using