C#Mysql UTF8编码
我有一个带有utf8_general_ci编码的mysql数据库,
我使用utf-8页面和文件编码连接到相同的数据库,没有问题但是当连接mysql与C#我有这样的字母غزة
我编辑连接字符串是这样的
server=localhost;password=root;User Id=root;Persist Security Info=True;database=mydatabase;Character Set=utf8
但同样的问题。
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CharSet=utf8;
注意! 使用小写值utf8而不是大写UTF8,因为这将失败。
请访问http://www.connectionstrings.com/mysql
你能尝试一下:
Server=localhost;Port=3306;Database=xxx;Uid=x xx;Pwd=xxxx;charset=utf8;"
编辑:我有了一个新想法:
//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);
祝好运
有关此技术的更多信息http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/BF68DDD8-3D95-4478-B84A-6570A2E20AE5
您可能需要为列使用“utf8mb4”字符集以支持4字节字符,如下所示:“λλ”
utf8字符集每个字符仅支持1-3个字节,因此不支持所有unicode字符。
有关详细信息,请参阅http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html 。
我找到的一件事,但没有机会真正浏览的是这里提供的整理图表: http : //www.collation-charts.org/mysql60/
这将显示哪些字符是给定MySQL排序规则的一部分,因此您可以为数据集选择最佳选项。
CHARSET应该是大写的
Server=localhost;Port=3306;Database=xxx;Uid=x xx;Pwd=xxxx;CHARSET=utf8;
以防万一有些人来这里。
我需要使用带有EF6的Mysql创建一个Seed方法来加载一个SQL文件。 运行后我在数据库上有奇怪的字符吗? 替换é,ó,á
解决方案:确保我使用正确的字符集读取文件:在我的情况下使用UTF8。
var path = System.AppDomain.CurrentDomain.BaseDirectory; var sql = System.IO.File.ReadAllText(path + "../../Migrations/SeedData/scripts/sectores.sql", Encoding.UTF8);
然后是M.Shakeri提醒:
在web.config中的cxn字符串上有CHARSET = utf8。 使用CHARSET作为大写和utf8小写。
希望能帮助到你。
R.
试试:
MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder(); conn_string.Server = "localhost"; conn_string.UserID = "root"; conn_string.Password = ""; conn_string.Database = "Learning"; conn_string.CharacterSet = "utf8"; conn = new MySqlConnection(conn_string.ToString()) ;
在连接字符串中设置charset是指发送到服务器的查询的字符集。 它不会影响从服务器返回的结果 。
https://dev.mysql.com/doc/connectors/en/connector-net-connection-options.html
我发现从客户端指定charset的一种方法是在打开连接后运行它。
set character_set_results='utf8';