Loading

MySqlBulkLoader 中文乱码

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文件的字符集
  • 需要指定StreamWriter字符集
  • 需要指定MySqlBulkLoader字符集

指定csv文件的字符集

写CSV文件借助的是开源包CsvHelper。
CsvHelper

指定 CsvHelper.Configuration.Configuration字符集

var configuration = new Configuration
{
     Encoding = Encoding.UTF8
 };

没能解决。

指定StreamWriter字符集

var writer = new StreamWriter(path, true, Encoding.UTF8)

不行。

指定MySqlBulkLoader字符集

bl.CharacterSet = "UTF8";

乱码解决。

posted @ 2019-06-12 18:23  repeatedly  阅读(872)  评论(1编辑  收藏  举报