简体   繁体   English

C# MySQL 库是否等效于 SQL SqlBulkCopy class?

[英]Does the C# MySQL library have an equivalent to the SQL SqlBulkCopy class?

I know there is MySqlBulkLoader, however it's not an equivalent as it isn't able to send data from memory and requires a file.我知道有 MySqlBulkLoader,但它不是等价物,因为它无法从 memory 发送数据并且需要一个文件。

This is a big problem for me because I want to bulk insert a huge amount of data into a MySQL database from a program that is already doing a lot of I/O.这对我来说是个大问题,因为我想从一个已经执行大量 I/O 的程序中将大量数据批量插入到 MySQL 数据库中。
I can't afford to also write millions of rows into a file and then have the MySqlBulkLoader read them back again, when I already have them in memory, it makes no sense.我不能再将数百万行写入一个文件,然后让 MySqlBulkLoader 再次读回它们,当我已经在 memory 中拥有它们时,这是没有意义的。

Why isn't there an option to do it directly from memory instead of using a file on disk?为什么没有直接从 memory 而不是使用磁盘上的文件的选项?

I suspect you use Oracle's Connector.NET.我怀疑您使用 Oracle 的 Connector.NET。 That library has several issues, mainly around async/await and Entity Framework support but one of them is the limited bulk load support.该库有几个问题,主要围绕异步/等待和实体框架支持,但其中之一是有限的批量加载支持。 MySQL allows bulk loading from the standard console. MySQL 允许从标准控制台批量加载。

Instead of Oracle's driver use the MySqlConnector package. That package is used by the most popular EF provider, Pomelo.EntityFrameworkCore.MySql with 22M downloads compared to Oracle's 900K downloads.使用MySqlConnector package 而不是 Oracle 的驱动程序。最流行的 EF 提供程序使用 package, Pomelo.EntityFrameworkCore.MySql与 Oracle 的 900K 下载相比,下载量为 22M。 MySqlConnector by itself has 35M downloads compared to Oracle's 38M.与 Oracle 的 38M 相比,MySqlConnector 本身有 35M 的下载量。

MySqlConnector allows bulk imports through its MySqlBulkCopy class which works similarly to SqlBulkCopy and accepts both DataTable and DataReader inputs. MySqlConnector 允许通过其MySqlBulkCopy class 进行批量导入,其工作方式与 SqlBulkCopy 类似,并接受 DataTable 和 DataReader输入。

var dataTable = GetDataTableFromExternalSource();

// open the connection
using var connection = new MySqlConnection("...;AllowLoadLocalInfile=True");
await connection.OpenAsync();

// bulk copy the data
var bulkCopy = new MySqlBulkCopy(connection);
bulkCopy.DestinationTableName = "some_table_name";
var result = await bulkCopy.WriteToServerAsync(dataTable);

MySqlConnector's core API is the same as Connector.NET. MySqlConnector的核心API与Connector.NET相同。 There's a migration guide as well that explains the differences.还有一个迁移指南解释了这些差异。 This guide includes the Connector.NET bugs that are fixed in MySqlConnector本指南包括在 MySqlConnector 中修复的 Connector.NET 错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM