整表模式比逐行插入要快得多,通常数数十万的数据几秒就导入完成,需要配置数据库local_infile=1,用整表模式,DataTable的名字需与数据库中表名对应
DataTable dt_appointment_time_config_day.TableName = "appointment_time_config_day"; BulkInsert(dt_appointment_time_config_day);
public void BulkInsert(DataTable table) { string pConStr = "server=rm-8vb90a1oibt820no13o.mysql.zhangbei.rds.aliyuncs.com;user id=feitu;password=feitu!@#$2020; port=3306; persist security info=True;database=feitu;AllowLoadLocalInfile=true;";//local_infile=1; MySqlConnection GetConnection = new MySqlConnection(pConStr); if (string.IsNullOrEmpty(table.TableName)) { throw new Exception("请给DataTable的TableName属性附上表名称"); } if (table.Rows.Count == 0) { return;//return 0; } int insertCount = 0; string tmpPath = System.IO.Path.GetTempFileName(); string csv = DataTableToCsv(table); StreamWriter sw = new StreamWriter(tmpPath, false, UTF8Encoding.UTF8); //要与mysql的编码方式对象, 数据库要utf8, 表也一样 sw.Write(csv); sw.Close(); // File.WriteAllText(tmpPath, csv); using (MySqlConnection conn = GetConnection) { MySqlTransaction tran = null; try { conn.Open(); tran = conn.BeginTransaction(); MySqlBulkLoader bulk = new MySqlBulkLoader(conn) { FieldTerminator = ",", FieldQuotationCharacter = '"', EscapeCharacter = '"', LineTerminator = " ", FileName = tmpPath, NumberOfLinesToSkip = 0, TableName = table.TableName, //也是mysql内表的名 }; // bulk.CharacterSet = "utf-8"; bulk.Columns.AddRange(table.Columns.Cast<DataColumn>().Select(colum => colum.ColumnName).ToList()); insertCount = bulk.Load(); tran.Commit(); } catch (MySqlException ex) { AppLog.Write(ex.ToString(), LogMessageType.Error); if (tran != null) tran.Rollback(); throw ex; } } File.Delete(tmpPath); //return insertCount; }
public void BulkInsert(DataTable table) {
string pConStr = "server=rm-8vb90a1oibt820no13o.mysql.zhangbei.rds.aliyuncs.com;user id=feitu;password=feitu!@#$2020; port=3306; persist security info=True;database=feitu;AllowLoadLocalInfile=true;";//local_infile=1; MySqlConnection GetConnection = new MySqlConnection(pConStr);
if (string.IsNullOrEmpty(table.TableName)) { throw new Exception("请给DataTable的TableName属性附上表名称"); }
if (table.Rows.Count == 0) { return;//return 0; }
int insertCount = 0; string tmpPath = System.IO.Path.GetTempFileName(); string csv = DataTableToCsv(table); StreamWriter sw = new StreamWriter(tmpPath, false, UTF8Encoding.UTF8); //要与mysql的编码方式对象, 数据库要utf8, 表也一样 sw.Write(csv); sw.Close(); // File.WriteAllText(tmpPath, csv); using (MySqlConnection conn = GetConnection) { MySqlTransaction tran = null; try { conn.Open(); tran = conn.BeginTransaction(); MySqlBulkLoader bulk = new MySqlBulkLoader(conn) { FieldTerminator = ",", FieldQuotationCharacter = '"', EscapeCharacter = '"', LineTerminator = "
", FileName = tmpPath, NumberOfLinesToSkip = 0, TableName = table.TableName, //也是mysql内表的名 }; // bulk.CharacterSet = "utf-8"; bulk.Columns.AddRange(table.Columns.Cast<DataColumn>().Select(colum => colum.ColumnName).ToList()); insertCount = bulk.Load(); tran.Commit(); } catch (MySqlException ex) { AppLog.Write(ex.ToString(), LogMessageType.Error); if (tran != null) tran.Rollback(); throw ex; } } File.Delete(tmpPath); //return insertCount;
}