LinkMenuExpand(external link)DocumentSearchCopyCopied

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 : DbBatch

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; }
override Timeout { get; set; }
Transaction { get; set; }
override Cancel()
override Dispose()
override ExecuteNonQuery()
override 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.
override ExecuteScalar()
override ExecuteScalarAsync(…)
override Prepare()
override PrepareAsync(…)

Protected Members

namedescription
override DbBatchCommands { get; }
override DbConnection { get; set; }
override DbTransaction { get; set; }
override CreateDbBatchCommand()
override ExecuteDbDataReader(…)
override ExecuteDbDataReaderAsync(…)

Remarks

The proposed ADO.NET API that MySqlBatch is based on is not finalized. This API is experimental and may change in the future.

See Also