21

我有一个带有 utf8_general_ci 编码的 mysql 数据库,

我正在使用 utf-8 页面和文件编码使用 php 连接到同一个数据库,没有问题,但是当使用 C# 连接 mysql 时,我有这样的字母غزة

我将连接字符串编辑为这​​样

server=localhost;password=root;User Id=root;Persist Security Info=True;database=mydatabase;Character Set=utf8

但同样的问题。

4

7 回答 7

37
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CharSet=utf8;

笔记!使用小写值 utf8 而不是大写 UTF8,因为这将失败。

http://www.connectionstrings.com/mysql

于 2012-07-27T13:54:30.467 回答
5

你能试试:

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

于 2012-07-27T15:07:28.940 回答
4

您可能需要为该列使用“utf8mb4”字符集以支持这样的 4 字节字符:“λ”

utf8 字符集仅支持每个字符 1-3 个字节,因此不能支持所有 unicode 字符。

有关详细信息,请参阅http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html 。

于 2013-04-19T21:51:26.813 回答
1

CHARSET 应该是大写的

Server=localhost;Port=3306;Database=xxx;Uid=x xx;Pwd=xxxx;CHARSET=utf8;
于 2014-07-27T11:00:18.440 回答
1

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 é, ó, á

SOLUTION: Make sure I read the file using the right charset: UTF8 on my case.

     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:

CHARSET=utf8 on cxn string in web.config. Using CHARSET as uppercase and utf8 lowercase.

Hope it helps.

R.

于 2017-06-26T17:45:10.580 回答
0

我发现但没有机会真正浏览的一件事是这里提供的整理图表:http: //www.collat​​ion-charts.org/mysql60/

这将向您显示哪些字符是给定 MySQL 排序规则的一部分,因此您可以为数据集选择最佳选项。

于 2012-07-27T15:32:09.980 回答
0

在连接字符串中设置字符集是指发送到服务器的查询的字符集。它不影响从服务器返回的结果。

https://dev.mysql.com/doc/connectors/en/connector-net-connection-options.html

我发现从客户端指定字符集的一种方法是在打开连接后运行它。

set character_set_results='utf8';

于 2018-06-08T19:19:35.247 回答