C# Mysql UTF8 Encoding

2020-01-29 06:47发布

I have a mysql database with utf8_general_ci encoding ,

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 غزة

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 .

7条回答
在下西门庆
2楼-- · 2020-01-29 07:17

You might need to use the "utf8mb4" character set for the column in order to support 4 byte characters like this: "λ

查看更多
戒情不戒烟
3楼-- · 2020-01-29 07:20
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.

See http://www.connectionstrings.com/mysql

查看更多
4楼-- · 2020-01-29 07:22

CHARSET should be uppercase

Server=localhost;Port=3306;Database=xxx;Uid=x xx;Pwd=xxxx;CHARSET=utf8;
查看更多
该账号已被封号
5楼-- · 2020-01-29 07:22

Setting the charset in the connection string refers to the charset of the queries sent to the server. It does not affect the results returned from the server.

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';

查看更多
够拽才男人
6楼-- · 2020-01-29 07:26

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/

This will show you which characters are part of a given MySQL collation so you can pick the best option for your dataset.

查看更多
干净又极端
7楼-- · 2020-01-29 07:31

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.

查看更多
登录 后发表回答