简体   繁体   English

如何使用C#批量插入MySQL

[英]How to bulk insert into MySQL using C#

I have previously used the SQLBulkCopy class to load data into a MS SQL Server db. 我以前使用SQLBulkCopy类将数据加载到MS SQL Server数据库中。 The results were very good, and worked exactly as I intended it to. 结果非常好,并且完全像我预期的那样工作。

Now, I'm trying to use a script task in SSIS to bulk load data into a MySQL (5.5.8) database using either an ODBC or ADO.NET connection (recommend?). 现在,我正在尝试使用SSIS中的脚本任务,使用ODBC或ADO.NET连接将数据批量加载到MySQL(5.5.8)数据库中(推荐?)。

The columns in my dataset correspond with the columns of the MySQL table. 我的数据集中的列对应于MySQL表的列。 What is the best way to do a bulk insert of a dataset into a MySQL database? 将数据集批量插入MySQL数据库的最佳方法是什么?

You can use the MySqlBulkLoader shipped with the MySQL Connector for .NET: 您可以使用MySQL Connector for .NET附带的MySqlBulkLoader:

var bl = new MySqlBulkLoader(connection);
bl.TableName = "mytable";
bl.FieldTerminator = ",";
bl.LineTerminator = "\r\n";
bl.FileName = "myfileformytable.csv";
bl.NumberOfLinesToSkip = 1;
var inserted = bl.Load();
Debug.Print(inserted + " rows inserted.");

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

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