1

I'm working with C# and MySQL now. I've tried to search around the internet for day to find out why I can't use AddWithValue method to add unicode characters because when I manually add it in MySQL, it works! But back in the C# code with MySQL connector for .NET it doesn't work. Other than the unicode characters is fine.

cmd.CommandText = "INSERT INTO tb_osm VALUES (@id, @timestamp, @user)";
cmd.Parameters.AddWithValue("@id", osmobj.ID);
cmd.Parameters.AddWithValue("@timestamp", osmobj.TimeStamp);
cmd.Parameters.AddWithValue("@user", osmobj.User);
cmd.ExecuteNonQuery();

For example: osmbj.User = "ສະບາຍດີ", it will be "???????" in the database. Please T^T

1
  • What is your database charset, do you specify that charset in the connection string?
    – GSerg
    Commented Apr 25, 2015 at 19:30

3 Answers 3

2

does this link help you?

read/write unicode data in MySql

Basically it says, you should append your connection string with charset=utf8;

Like so: id=my_user;password=my_password;database=some_db123;charset=utf8;

1
  • Thank you very much this saves me a lot :) Commented Apr 26, 2015 at 15:52
0

You have to be sure that unicode characters are supported at every level of the process, all the way from the input into C# to the column stored in MySql.

The C# level is easy, because strings are already utf-16 by default. As long as you're not using some weird gui toolkit, reading from a bad file or network stream, or running in a weird console app environment with no unicode support, you'll be in good shape.

The next layer is the parameter definition. Here, you're better off avoiding the AddWithValue() method, anyway. The link pertains the Sql Server, but the same reasoning applies to MySql, even if MySql is less strict with your data than it should be. You should use an Add() override that lets you explicitly the declare the type of your parameters as NVarChar, instead of making the ADO.Net provider try to guess.

Next up is the connection between your application and the database. Here, you want to make sure to include the charset=utf8 clause (or better) as part of the connection string.

Then we need to think about the collation of the database itself. You have to be sure that an NVarChar column in MySql will be able to support your data. One of the answers from the question at previous link also covers how to handle this.

Finally, make sure the column is defined with the NVarChar type, instead of just VarChar.

1
  • yeah, the charset=utf8; is what I want. Thank you for your support :) Commented Apr 26, 2015 at 15:52
0

Yes, utf8 at all stages -- byte-encoding in client, conversion on the wire (charset=utf8), and on the column. I do not know whether C# converts from utf16 to utf8 before exposing the characters; if it does not, then charset=utf16 (or no setting) may be the correct step.

Because you got multiple ?, the likely cause is trying to transform non-latin1 characters into a CHARACTER SET latin1 column. Since latin1 has no codes for Lao, ? was substituted. Probably you said nothing about the column, but depended on the DEFAULT on the table and/or database, which happened to be latin1.

The ສະບາຍດີ is lost and cannot be recovered from ???????.

Once you have changed things, check that it is stored correctly by doing SELECT col, HEX(col) .... For the string ສະບາຍດີ, you should get hex E0BAAAE0BAB0E0BA9AE0BAB2E0BA8DE0BA94E0BAB5. Notice how that is groups of E0BAxx, which is the range of utf8 values for Lao.

If you still have troubles, please provide the HEX for further analysis.

1
  • thanks for your help and giving me new knowledge, but this charset=utf8; saves me :) Commented Apr 26, 2015 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.