简体   繁体   中英

c# latin1_swedish_ci to unicode

I have a MySQL database with latin1_swedish_ci and when I'm trying to select something, select returns strings like this:

ტáƒáƒ‘ი მáƒáƒ™áƒ’უáƒáƒ˜áƒ áƒ˜, კირსტენ დáƒáƒœáƒ¡áƒ¢áƒ˜, ჯეიმს ფრáƒáƒœáƒ™áƒ

Here is my code:

string db = "server=xxx;database=xxx;uid=xxx;password=xxx;charset=utf8;";
MySqlConnection con = new MySqlConnection(db);
con.Open();
MySqlCommand cmd = new MySqlCommand("SET NAMES utf8; SELECT * FROM `xxx`", con);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
     string source = reader.GetString("xxx");
     byte[] utf8Bytes = Encoding.UTF8.GetBytes(source);
     byte[] isoBytes = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, utf8Bytes);
     string uf8converted = Encoding.UTF8.GetString(isoBytes);    
}

What am I doing wrong? Please help.

I know this question is a year old, but I had the same problem and found a solution:

string source = reader.GetString("xxx");
Encoding enc = Encoding.GetEncoding(1252);
byte[] data = enc.GetBytes(source);
string uf8converted = Encoding.UTF8.GetString(data);

The latin1_swedish_ci is codified as Western European (not ASCII), so for the result we hardcode the bytes in UTF8 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM