更改对象列表的编码 [英] Change encoding of a list of objects

查看:205
本文介绍了更改对象列表的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它使用Dapper从SQL Server的数据读取,然后将数据保存到另一个MySQL数据库。它正在做的是:

I have a program which using Dapper read in data from SQL Server and then save the data into another MySQL database. What it is doing is:


  1. 从SQL服务器以对象列表的形式读入数据。 List< AKindOfObject> = 从SQL Server中读取数据

  2. 打开与MySQL的连接,保存 List< AKindOfObject>

  1. Read in data from SQL server as a list of objects e.g. List<AKindOfObject> = read in data from SQL Server
  2. Open a connection to MySQL, save the List<AKindOfObject>

问题是 AKindOfObject 的一些属性在原始数据库中保存为中文。当数据传输到MySQL数据库时,这些中文字符显示为?。

The problem is some property of AKindOfObject is saved as Chinese in original database. When the data is transferred to the MySQL database, those Chinese characters are shown as "?".

有任何方法可以更改列表< AKindOfObject> 到C#代码中的gb2312?

Is there any way to change the encoding of List<AKindOfObject> to gb2312 in C# code?

推荐答案

>这里发生的是简单的:你的数据库有非unicode列,并且你正试图存储unicode数据。不可表示的字符将会被确认。正确的修复是:确保您的列是unicode。使用列编码可以工作,但是 与仅使用unicode相比,工作量很大。

I suspect what is happening here is simply: your database has non-unicode columns, and you are trying to store unicode data. Unrepresentable characters will indeed be greeked. The correct fix there is: make sure your column is unicode. Messing with column encodings could work, but damn that's a lot of work compared to just using unicode.

Dapper将事情按原样传递给ADO.NET;提供者做什么取决于提供者。 是通过 DbString ,即

Dapper passes things "as is" to ADO.NET; what the provider does is up to the provider. There is a way to tell it to choose between ANSI and Unicode when sending data into the database, though - via DbString, i.e.

conn.Execute(sql, new {
    id, name,
    desc = new DbString { IsAnsi = true, Value = desc }
});

这也允许你控制长度等。但是,我们不是控制这里;编码通常是数据库本身或提供者的属性。如果MySQL有一些定制的方式来控制在ADO.NET,我是耳朵,但首先:你需要得到它在原始的ADO.NET工作。

This also allows you to control the length etc. However, we are not controlling the encoding here; the encoding is usually a property of the database itself, or of the provider. If MySQL has some bespoke ways of controlling that in ADO.NET, I'm "all ears", but first: you need to get it working in raw ADO.NET.

这篇关于更改对象列表的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆