MySQL的C#文本编码问题 [英] MySQL C# Text Encoding Problems
问题描述
我有编码设置为UTF-8旧的MySQL数据库。我使用Ado.Net实体框架连接到它。
I have an old MySQL database with encoding set to UTF-8. I am using Ado.Net Entity framework to connect to it.
这是我检索它有奇怪的字符ë像字符按照预期的字符串。
The string that I retrieve from it have strange characters when ë like characters are expected.
例如:E是«
我想我可以从UTF8转换为UTF16得到这个权利。
I thought I could get this right by converting from UTF8 to UTF16.
return Encoding.Unicode.GetString(
Encoding.Convert(
Encoding.UTF8,
Encoding.Unicode,
Encoding.UTF8.GetBytes(utf8)));
}
然而,这不会改变任何事情。
This however doesn't change a thing.
我怎么能得到从这个数据库中的数据以适当的形式?
How could I get the data from this database in proper form?
推荐答案
有,你需要做的,支持UTF-8在ADO.NET实体框架的工作两件事情(或一般使用MySQL .NET连接器)
There are two things that you need to do to support UTF-8 in the ADO.NET Entity frame work (or in general using the MySQL .NET Connector):
- 请确保您的表的数据库的归类是UTF-8的排序规则(即
utf8_general_ci
或它的关系之一) -
添加
字符集= UTF8;
来连接字符串。
- Ensure that the collation of your database of table is a UTF-8 collation (i.e.
utf8_general_ci
or one of its relations) Add
Charset=utf8;
to your connection string.
"Server=localhost;Database=test;Uid=test;Pwd=test;Charset=utf8;"
我不能肯定,但编码可能是大小写敏感的; 我发现字符集= UTF8;
我没有工作
I'm not certain, but the encoding may be case sensitive; I found that CharSet=UTF8;
did not work for me.
这篇关于MySQL的C#文本编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!