1

ERROR C# MVC5 using ADO.NET when update columns using Chinese in mysql(version:5.7.27-0ubuntu0.18.04.1 - (Ubuntu)): {"Incorrect string value: '\xE4\xB8\xAD\xE6\x96\x87' for column 'Title_1' at row 1"}

I have already tried charset for database, table and columns.

(1)'utf8' 'utf8_general_ci'
(2)'utf8' 'utf8_unicode_ci'
(3)'utf8mb4' 'utf8mb4_general_ci'
(4)'utf8mb4' 'utf8mb4_unicode_ci'

Also,I've added charset=utf8mb4 or charset=utf8 in connectionstring in Web.config

my connection string is

<add name="constr" connectionString="Data Source=ServerIP;port=3306;Initial Catalog=jn001;User Id=Userid;password=Password;charset=utf8mb4">

Google all day finding answers, Still can't find a solution, Please tell me a solution,Thanks!

here is my mysql charset


I've followed LocEngineer's advertisement and set mysql server to utf8mb4

here is my new mysql charset

And this is my collation

All change to utf8mb4 (including connectionstring) ,but still the same error occur.

Anyway ,thanks for the suggestions.

------------------------------9/5update--------------------------------

my SHOW CREATE TABLE

 public bool UpdateDetails(Class1 content)
        {
            connection();
            MySqlCommand cmdSet = new MySqlCommand("set names utf8mb4", con);
            MySqlCommand cmd = new MySqlCommand("update", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Title_1",content.Title_1);
            cmd.Parameters.AddWithValue("@Title_2", content.Title_2);
            cmd.Parameters.AddWithValue("@Title_3", content.Title_3);
            cmd.Parameters.AddWithValue("@Content_1", content.Content_1);
            cmd.Parameters.AddWithValue("@Content_2", content.Content_2);
            cmd.Parameters.AddWithValue("@Content_3", content.Content_3);

            con.Open();
            cmdSet.ExecuteNonQuery();
            int i = cmd.ExecuteNonQuery();
            con.Close();
            if (i >= 1)
                return true;
            else
                return false;
        }

this is mysql update Procedure

UPDATE Stop_Info SET Title_1 = Title_1,Title_2=Title_2,Title_3=Title_3,Content_1=Content_1,Content_2=Content_2,Content_3=Content_3 WHERE Pkey = 3

HTML form accept-charset="utf-8"

<form id="registerForm" method="post" action="~/Home/Index" class="form-horizontal" accept-charset="utf-8">
12
  • 1
    Did you specify the useUnicode in your connection string ? --> useUnicode=true&characterEncoding=utf8 Commented Sep 4, 2019 at 8:41
  • here is my connection string , <add name="constr" connectionString="Data Source=ServerIP;port=3306;Initial Catalog=jn001;User Id=Userid;password=Password@;charset=utf8"/> Commented Sep 4, 2019 at 8:48
  • Olivier Depriester {"Option not supported.\r\n: useunicode"} seems like useUnicode error Commented Sep 4, 2019 at 8:53
  • Sorry, I gave you jdbc connection string parameters. They do not apply to c# Commented Sep 4, 2019 at 9:10
  • 1
    It seems that utf8 is a bit of a misnomer in mySql. You need to use utf8mb4, both in your database but also in your connection string. Check out mathiasbynens.be/notes/mysql-utf8mb4 and stackoverflow.com/questions/28540772/using-utf8mb4-in-mysql . Commented Sep 4, 2019 at 9:11

2 Answers 2

1

\xE4\xB8\xAD\xE6\x96\x87 is hex for 中文 ("Chinese language").

In MySQL, this will work with either utf8 or utf8mb4. However, since there are several Chinese characters that need 4 bytes, you are correct to use utf8mb4. Meanwhile, the COLLATION (eg, utf8mb4_unicode_ci) does not matter to the Question in hand.

The individual column's charset is important, not the database's. And the 191 kludge is needed only in 5.5 and 5.6. Please provide SHOW CREATE TABLE.

Another way to establish the connection parameters is to issue SET NAMES utf8mb4 immediately after connecting. (This is not preferred, but might be worth trying. It should be a workaround for the Gorm issue mentioned.)

Are you getting the error message when connecting? Or when issuing a particular query? If so, please provide that query.

Check that skip-character-set-client-handshake is not being used.

Check the "Best practice" in https://stackoverflow.com/a/38363567/1766831

Addenda Since you mentioned a stored procedure, I suggest you do SHOW CREATE PROCEDURE to see what charset it was built with. Here's an example of what can happen:

mysql> SET NAMES latin1;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE PROCEDURE x () BEGIN END ;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW CREATE PROCEDURE x \G
*************************** 1. row ***************************
           Procedure: x
            sql_mode: NO_ENGINE_SUBSTITUTION
    Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `x`()
BEGIN END
character_set_client: latin1
collation_connection: latin1_swedish_ci
  Database Collation: utf8mb4_unicode_520_ci
1 row in set (0.00 sec)

mysql> DROP PROCEDURE x;
Query OK, 0 rows affected (0.01 sec)

versus:

mysql> SET NAMES utf8mb4;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE PROCEDURE x () BEGIN END ;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW CREATE PROCEDURE x \G
*************************** 1. row ***************************
           Procedure: x
            sql_mode: NO_ENGINE_SUBSTITUTION
    Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `x`()
BEGIN END
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
  Database Collation: utf8mb4_unicode_520_ci
1 row in set (0.00 sec)

If you don't see utf8mb4 on your PROCEDURE and FUNCTION declarations, rebuild them.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks and sorry for the late, I've update SHOW CREATE TABLE and some code , please check my content above.
I've tried the link and made some change to my code,can't still solve the problem
@jimmyan - Stored Proc was the clue I needed. See my Addenda.
OMG! I'm so dumb , forget to change utf8 in my PROCEDURE. I've solved the problem!! Thanks for reminding , saving my day.
@jimmyan - You are welcome. (And click the check mark.)
1

This didnt work for me. After days of fiddling and trying practically everything, I had to add charset utf8mb4 to the parameter declaration in the stored proc. param text charset utf8mb4.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.