C# Mysql UTF8 Encoding
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 .
Note! Use lower case value utf8 and not upper case UTF8 as this will fail.
could you try:
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);
more info about this technique http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/BF68DDD8-3D95-4478-B84A-6570A2E20AE5
You might need to use the "utf8mb4" character set for the column in order to support 4 byte characters like this: "λ𝛌 "
The utf8 charset only supports 1-3 bytes per character and thus can't support all unicode characters.
See http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html for more details.
CHARSET should be uppercase
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.
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.
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.
One way I have found to specify the charset from the client is to run this after opening the connection.