MySqlConnector


MySqlBatch class

MySqlBatch implements the new ADO.NET batching API. It is currently experimental and may change in the future.

When using MariaDB (10.2 or later), the commands will be sent in a single batch, reducing network round-trip time. With other MySQL Servers, this may be no more efficient than executing the commands individually.

Example usage:

using var connection = new MySqlConnection("...connection string...");
await connection.OpenAsync();

using var batch = new MySqlBatch(connection)
{
	BatchCommands =
	{
		new MySqlBatchCommand("INSERT INTO departments(name) VALUES(@name);")
		{
			Parameters =
			{
				new MySqlParameter("@name", "Sales"),
			},
		},
		new MySqlBatchCommand("SET @dept_id = last_insert_id()"),
		new MySqlBatchCommand("INSERT INTO employees(name, department_id) VALUES(@name, @dept_id);")
		{
			Parameters =
			{
				new MySqlParameter("@name", "Jim Halpert"),
			},
		},
	 	new MySqlBatchCommand("INSERT INTO employees(name, department_id) VALUES(@name, @dept_id);")
		{
			Parameters =
			{
				new MySqlParameter("@name", "Dwight Schrute"),
			},
		},
	},
 };
 await batch.ExecuteNonQueryAsync();
public sealed class MySqlBatch : IDisposable

Public Members

namedescription
MySqlBatch()Initializes a new MySqlBatch object. The Connection property must be set before this object can be used.
MySqlBatch(…)Initializes a new MySqlBatch object, setting the Connection and Transaction if specified.
BatchCommands { get; }The collection of commands that will be executed in the batch.
Connection { get; set; }
Timeout { get; set; }
Transaction { get; set; }
Cancel()
Dispose()
ExecuteNonQuery()
ExecuteNonQueryAsync(…)
ExecuteReader()Executes all the commands in the batch, returning a MySqlDataReader that can iterate over the result sets. If multiple resultsets are returned, use NextResult to access them.
ExecuteReaderAsync(…)Executes all the commands in the batch, returning a MySqlDataReader that can iterate over the result sets. If multiple resultsets are returned, use NextResultAsync to access them.
ExecuteScalar()
ExecuteScalarAsync(…)
Prepare()
PrepareAsync(…)

See Also