MySql MySqlBulkLoader批量导入数据(批导BulkInsert)
MySql MySqlBulkLoader批量导入数据(批导BulkInsert)
引言
MySql批导需要将数据存储到本地csv文件,然后使用MySqlBulkLoader导入数据。
BulkInsert批导方法
C# 全选
public override int BulkInsert<T>(List<T> entities, string tableName)
{
using (MySqlConnection conn = new MySqlConnection())
{
conn.ConnectionString = ConnectionString;
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
//开启本地文件开关,否则报错:https://www.cscode.net/archive/newdoc/605188819611653.html
var cmd = conn.CreateCommand();
cmd.CommandText = "set global local_infile = 1;";
cmd.ExecuteNonQuery();
if (String.IsNullOrWhiteSpace(tableName))
{
var tableAttribute = typeof(T).GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault();
if (tableAttribute != null)
tableName = ((TableAttribute)tableAttribute).Name;
else
tableName = typeof(T).Name;
}
int insertCount = 0;
string tmpPath = DateTime.Now.Ticks.ToString() + ".csv";
DataTable dt = entities.ToDataTable(tableName);
var csv = dt.ToCsv2();
//要与mysql的编码方式对象, 数据库要utf8, 表也一样
using (StreamWriter sw = new StreamWriter(tmpPath, false, UTF8Encoding.UTF8))
{
sw.Write(csv);
sw.Close();
}
MySqlBulkLoader bulk = new MySqlBulkLoader(conn)
{
FieldTerminator = ",",
FieldQuotationCharacter = '"',
EscapeCharacter = '"',
LineTerminator = "\r\n",
NumberOfLinesToSkip = 0,
FileName = tmpPath,
TableName = tableName,
ConflictOption = MySqlBulkLoaderConflictOption.Replace,
Priority = MySqlBulkLoaderPriority.None,
Timeout = 1000 * 60 * 60,//milliseconds
CharacterSet = "utf8",
};
try
{
bulk.Columns.AddRange(dt.Columns.Cast<DataColumn>().Select(colum => colum.ColumnName).ToList());
insertCount = bulk.Load();
}
catch (MySqlException ex)
{
conn.Close();
throw;
}
finally
{
if (File.Exists(tmpPath)) File.Delete(tmpPath);
}
return insertCount;
}
}
DataTable转csv文件
C# 全选
/// <summary>
/// 将DataTable转换为标准的CSV字符串(空值转换为NULL)
/// </summary>
/// <param name="dataTable"></param>
/// <param name="createTitle">是否添加标题</param>
/// <returns></returns>
public static string ToCsv2(this DataTable dataTable, bool createTitle = false)
{
var builder = new StringBuilder();
// 添加列标题
if (createTitle)
{
var columnTitles = string.Join(",", dataTable.Columns.Cast<DataColumn>().Select(column => $"\"{column.ColumnName}\""));
builder.AppendLine(columnTitles);
}
// 添加数据行
foreach (DataRow row in dataTable.Rows)
{
var fields = row.ItemArray.Select(field => field is DBNull ? "NULL" : $"\"{field.ToString().Replace("\"", "\"\"")}\"");
var line = string.Join(",", fields);
builder.AppendLine(line);
}
return builder.ToString();
}
CSFramework.EF数据库框架
CSFramework.EF是轻量级数据库底层框架,基于Entity Framework 实体框架强大功能封装而成。支持三种主流数据库,分别是SqlServer、Oracle、MySQL,支持国产数据库 - 达梦数据库,用户可扩展其他数据如 PostgreSQL,MongoDB,SQLLite等。
CSFramework.EF数据库框架提供IDatabase接口,里面定义了一组通用的接口方法,如增、删、改、查:Add<T>, Update<T>,Remove<T>,GetQuaryable<T>,支持LINQ,SQL脚本查询和操作,支持常用事务、BulkInsert批量插入等功能。
软件介绍:https://www.cscode.net/archive/csframework.ef/363596745297925.html
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网