C # with MySql and Unicode characters

My problem is using C # and a MySQL database to save some records by sending parameters.

Although I have already set the encoding as Utf-8, and I can correctly see Unicode characters, the problem I get when trying to insert Unicode characters is that it only saves half the string.

It is really strange that this happens only with Unicode strings, such as Greek words, and only when I send a request with parameters.

Those. if my request, as shown in C #, is as follows:

string query = "INSERT INTO tablename VALUES (NULL, @somestring)"; 

and I set the @somestring parameter value to "TESTING". It will be very good.

If I try to set the value as a Unicode string "ΤΕΣΤΙΝΓ", the query is executed perfectly without errors, but it only stores half the characters in the database, i.e. he only saves "ΤΕΣΤ".

On the other hand, if I remove the parameters and configure the query as:

 string somestring = "ΤΕΣΤΙΝΓ"; string query = "INSERT INTO tablename VALUES (NULL,'" + somestring + "')"; 

the query again works just fine and saves the entire word / sentence in the database.

Hope I explained it correctly and you can understand my situation.

thanks

+4
source share
1 answer

The length of how you declare the @somestring parameter is too short in C #.

UTF-8 takes up to 3 bytes per character, so you will need a length of not 21, but 7, for example, for testing and variations.

Saying this, I did not use C # to call MySQL (SQL Server only), but I'm sure this is a problem

+5
source

Source: https://habr.com/ru/post/1392217/


All Articles