user2277775
user2277775

Reputation: 53

c# mysql select with where condition

WHERE status= 'โกดัง' " I try this it does not work ( it is utf8 (Thai language) Do I need to set up something in C# to make it know is utf8 or be cause of I can not use 'โกดัง'

I use c# and mysql the code is work until I fill WHERE status= 'โกดัง' before String cmdText = "SELECT * FROM trackingbymail "; it work

String str = @"server=localhost;database=asianimport;userid=tga;password=se57ui849;";


String cmdText = "SELECT * FROM trackingbymail WHERE  status=  'โกดัง' ";
                MySqlCommand cmd = new MySqlCommand(cmdText, con);
                MySqlDataReader reader = cmd.ExecuteReader();

Upvotes: 0

Views: 801

Answers (2)

Conrado Costa
Conrado Costa

Reputation: 435

  1. Ensure that the collation of your database of table is a UTF-8 collation (i.e. utf8_general_ci or one of its relations)

  2. Add Charset=utf8; to your connection string.

"Server=localhost;Database=test;Uid=test;Pwd=test;Charset=utf8;"

Source: MySQL C# Text Encoding Problems

Anyway if it can't solve, you can store the bytes instead the string. But probably it isn't the best way to solve that...

UTF8Encoding utf8 = new UTF8Encoding();
string unicodeString = "โกดัง";
byte[] encodedBytes = utf8.GetBytes(unicodeString);

then when you need you could back to string:

Encoding.UTF8.GetString(encodedBytes);

Upvotes: 2

emirc
emirc

Reputation: 2018

You should save your source code file as UTF-8.

Have you tried this?

Upvotes: 0

Related Questions