Click here to Skip to main content
15,746,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When write on Blob field with Encoding UTF 8 , the caracter write are wrong

table tb_Language
charset/collation utf8 -- utf8_bin
engine InnoDb

if insert the string by hand with MySqlWorkBench the result is correct

What I have tried:

MySqlCommand cmdMySqlN = new MySqlCommand("", Connection);
cmdMySqlN.CommandText = "INSERT INTO tb_Language (Text) VALUES (@text)"; ;

                myTrans = Connection.BeginTransaction();

                Encoding utf8 = Encoding.UTF8;

                //Unicode string:
                string test = "abcéabc Colón à @ ç § ";                                

                byte[] utfBytes = utf8.GetBytes(test);

                //parameter for Blob
                cmdMySqlN.Parameters.AddWithValue("@text", utfBytes);

                cmdMySqlN.Transaction = myTrans;


            catch (Exception ex)
                string Message = "Error " + ex.Message;

read with MySqlWorkBench
and the result is <pre>abcéabc Colón à @ ç §

may be here the error
byte[] utfBytes = utf8.GetBytes(testo);
Mehdi Gholam 18-Apr-19 1:35am    
What is the value when you read from c#?
Richard MacCutchan 18-Apr-19 3:59am    
The problem is the character set you are using to display them. If you check the actual byte values of the data they will be the same as when you saved them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900