C# Mysql UTF8 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11689129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Mysql UTF8 Encoding
提问by T4mer
I have a mysql database with utf8_general_ci encoding ,
我有一个 utf8_general_ci 编码的 mysql 数据库,
i'm connecting to the same database with php using utf-8 page and file encode and no problem but when connection mysql with C# i have letters like this ?o?2??
我正在使用 utf-8 页面和文件编码使用 php 连接到同一个数据库,没有问题,但是当使用 C# 连接 mysql 时,我有这样的字母?o?2??
i editit the connection string to be like this
我将连接字符串编辑为这样
server=localhost;password=root;User Id=root;Persist Security Info=True;database=mydatabase;Character Set=utf8
but the same problem .
但同样的问题。
回答by x06265616e
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CharSet=utf8;
Note! Use lower case value utf8 and not upper case UTF8 as this will fail.
笔记!使用小写值 utf8 而不是大写 UTF8,因为这会失败。
回答by Anthony
could you try:
你可以试试:
Server=localhost;Port=3306;Database=xxx;Uid=x xx;Pwd=xxxx;charset=utf8;"
Edit: I got a new idea:
编辑:我有了一个新想法:
//To encode a string to UTF8 encoding
string source = "hello world";
byte [] UTF8encodes = UTF8Encoding.UTF8.GetBytes(source);
//get the string from UTF8 encoding
string plainText = UTF8Encoding.UTF8.GetString(UTF8encodes);
good luck
祝你好运
more info about this technique http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/BF68DDD8-3D95-4478-B84A-6570A2E20AE5
回答by Robert H
One thing I found, but haven't had the opportunity to really browse is the collation charts available here: http://www.collation-charts.org/mysql60/
我发现但没有机会真正浏览的一件事是这里提供的整理图表:http: //www.collation-charts.org/mysql60/
This will show you which characters are part of a given MySQL collation so you can pick the best option for your dataset.
这将显示哪些字符是给定 MySQL 排序规则的一部分,以便您可以为数据集选择最佳选项。
回答by Bryan Legend
You might need to use the "utf8mb4" character set for the column in order to support 4 byte characters like this: "λ "
您可能需要为该列使用“utf8mb4”字符集以支持这样的 4 字节字符:“λ”
The utf8 charset only supports 1-3 bytes per character and thus can't support all unicode characters.
utf8 字符集仅支持每个字符 1-3 个字节,因此不能支持所有 unicode 字符。
See http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.htmlfor more details.
有关更多详细信息,请参阅http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html。
回答by M.Shakeri
CHARSET should be uppercase
CHARSET 应该是大写的
Server=localhost;Port=3306;Database=xxx;Uid=x xx;Pwd=xxxx;CHARSET=utf8;
回答by Ramon Gonzalez
Just in case some come here later.
以防万一有人稍后来这里。
I needed to create a Seed method using Mysql with EF6, to load a SQL file. After running it I got weird characters on database like ? replacing é, ó, á
我需要使用 Mysql 和 EF6 创建一个 Seed 方法,以加载 SQL 文件。运行它后,我在数据库上得到了奇怪的字符,例如?替换 é, ó, á
SOLUTION:Make sure I read the file using the right charset: UTF8 on my case.
解决方案:确保我在我的情况下使用正确的字符集读取文件:UTF8。
var path = System.AppDomain.CurrentDomain.BaseDirectory;
var sql = System.IO.File.ReadAllText(path + "../../Migrations/SeedData/scripts/sectores.sql", Encoding.UTF8);
And then M.Shakeri reminder:
然后 M.Shakeri 提醒:
CHARSET=utf8 on cxn string in web.config. Using CHARSET as uppercase and utf8 lowercase.
web.config 中 cxn 字符串上的 CHARSET=utf8。使用 CHARSET 作为大写和 utf8 小写。
Hope it helps.
希望能帮助到你。
R.
R。
回答by Kevin Tighe
Setting the charset in the connection string refers to the charset of the queries sent to the server. It does not affect the resultsreturned from the server.
在连接字符串中设置字符集是指发送到服务器的查询的字符集。它不会影响从服务器返回的结果。
https://dev.mysql.com/doc/connectors/en/connector-net-connection-options.html
https://dev.mysql.com/doc/connectors/en/connector-net-connection-options.html
One way I have found to specify the charset from the client is to run this after opening the connection.
我发现从客户端指定字符集的一种方法是在打开连接后运行它。
set character_set_results='utf8';
set character_set_results='utf8';

