MySQL C# 文本编码问题 [英] MySQL C# Text Encoding Problems
问题描述
我有一个旧的 MySQL 数据库,编码设置为 UTF-8.我正在使用 Ado.Net Entity 框架连接到它.
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.
例如:ë"是ë".
我认为我可以通过从 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?
推荐答案
要在 ADO.NET 实体框架(或通常使用 MySQL .NET 连接器)中支持 UTF-8,您需要做两件事:
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
或其关系之一) 将
Charset=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;"
我不确定,但编码可能区分大小写;我发现 CharSet=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屋!