MySqlConnector


MySqlBulkCopy

MySqlBulkCopy lets you efficiently load a MySQL Server Table with data from another source. It is similar to the SqlBulkCopy class for SQL Server.

Due to security features in MySQL Server, the connection string must have AllowLoadLocalInfile=true in order to use this class.

Example Code

// NOTE: to copy data between tables in the same database, use INSERT ... SELECT
// https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
var dataTable = GetDataTableFromExternalSource();

using (var connection = new MySqlConnection("...;AllowLoadLocalInfile=True"))
{
	await connection.OpenAsync();
	var bulkCopy = new MySqlBulkCopy(connection);
	bulkCopy.DestinationTableName = "some_table_name";
	await bulkCopy.WriteToServerAsync(dataTable);
}

API Reference

Constructors

public MySqlBulkCopy(MySqlConnection connection, MySqlTransaction transaction = null)

Initializes a MySqlBulkCopy with the specified connection, and optionally the active transaction.

Properties

public int BulkCopyTimeout { get; set; }

The number of seconds for the operation to complete before it times out, or 0 for no timeout.

public string DestinationTableName { get; set; }

Name of the destination table on the server.

Methods

public void WriteToServer(DataTable dataTable);

public Task WriteToServerAsync(DataTable dataTable, CancellationToken cancellationToken = default);

Copies all rows in the supplied DataTable to the destination table specified by the DestinationTableName property of the MySqlBulkCopy object. (This method is not available on netstandard1.3.)


public void WriteToServer(IDataReader dataReader);

public Task WriteToServerAsync(IDataReader dataReader, CancellationToken cancellationToken = default);

Copies all rows in the supplied IDataReader to the destination table specified by the DestinationTableName property of the MySqlBulkCopy object.