C #: Unicode from a string with MySQL

I am trying to insert a row into a MySQL database. I can insert it by running a request on the server, but when I try to use the C # source file to insert “Iñtërnâtiônàlizætiøn”, I get “Ià ± tÃ" rnà ¢ tiÃ'nà lizà | tiøn ". I tried to add it as parameter and add: charset = utf8 to the connection string, but no luck. The table in the database has utf8 as the character set. I missed something.

This is my code (using StringBuilder):

sqlBuffer.Append( string.Format( @"INSERT `resources` (id, somefield) VALUES (20004,'Iñtërnâtiônàlizætiøn');"); 
+4
source share
2 answers

You need to indicate that it is unicode / utf8 with N '' or _utf8 '':

 sqlBuffer.Append( string.Format( @"INSERT `resources` (id, somefield) VALUES (20004, N'Iñtërnâtiônàlizætiøn');"); 
0
source

You probably need to select the right culture and provide it with string.Format. You can read http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx which inspired the example below (WARNING: untested!):

 using System.Globalization; // ... string[] cultureNames = { "en-US", "fr-FR", "de-DE", "es-ES" }; // this loop will inject your string using various cultures foreach (string cultureName in cultureNames) { CultureInfo culture = new CultureInfo(cultureName); sqlBuffer.Append( string.Format(culture, @"INSERT `resources` (id, somefield) VALUES (20004, N'Iñtërnâtiônàlizætiøn');"); } 

Note that you also need to check your table settings for sorting character sets.

0
source

All Articles