zoukankan      html  css  js  c++  java
  • 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";
    

    乱码解决。

  • 相关阅读:
    30-JDBC(2)
    29-JDBC(1)
    27-网络编程
    26-IO(中)
    git push 报错
    IsEmpty和isBlank区别
    java.lang.NumberFormatException: For input string: "0.9"
    Integer与Double类型转换
    Lambda 表达式排序
    Number & Math 类方法
  • 原文地址:https://www.cnblogs.com/AlienXu/p/11011394.html
Copyright © 2011-2022 走看看