时间:2021-07-01 10:21:17 帮助过:9人阅读
MySQL驱动:MySqlConnector
GitHub地址:https://github.com/mysql-net/MySqlConnector.git
文档地址:https://mysql-net.github.io/MySqlConnector/
代码插入之后发现出现中文乱码现象。
实现逻辑梳理,将数组写入csv,通过MySqlBulkLoader批量插入
public async Task BulkLoadLocalCsvFile<T>(IEnumerable<T> dataSource, string tableName)
{
using (var connection = new MySqlConnection(GetLocalConnectionString()))
{
await connection.OpenAsync();
var path = Path.GetTempFileName();
MySqlBulkLoader bl = new MySqlBulkLoader(connection);
bl.FileName = path;
bl.TableName = tableName;
bl.FieldTerminator = ",";
bl.FieldQuotationCharacter = '"';
bl.FieldQuotationOptional = true;
bl.Local = true;
using (var writer = new StreamWriter(path))
{
var configuration = new Configuration
{
HasHeaderRecord = false,
};
using (var csv = new CsvWriter(writer, configuration))
{
csv.WriteRecords(dataSource);
var writerConfiguration = csv.Context.WriterConfiguration;
var map = writerConfiguration.Maps.Find<T>();
foreach (var memberMap in map.MemberMaps)
{
var memberName = memberMap.Data.Member.Name;
loader.Columns.Add(memberName);
}
}
}
int rowCount = await bl.LoadAsync();
File.Delete(path);
}
}
写CSV文件借助的是开源包CsvHelper。
CsvHelper
指定 CsvHelper.Configuration.Configuration
字符集
var configuration = new Configuration
{
Encoding = Encoding.UTF8
};
没能解决。
var writer = new StreamWriter(path, true, Encoding.UTF8)
不行。
bl.CharacterSet = "UTF8";
乱码解决。
MySqlBulkLoader 中文乱码
标签:插入 文档 实现 temp coding nuget 原因 err await