I'm trying to insert a string into a MySQL database. I can insert it by running the query on the server, but when I try to use my C# source file to insert "Iñtërnâtiônàlizætiøn", I get "Iñtërnâtiônà lizætiøn". I've tried adding it as a parameter and adding ;charset=utf8 to my connection string, but no luck. The table in the database has utf8 as it's character set. Am I missing something.
This is my code (using a StringBuilder):
sqlBuffer.Append(
string.Format(
@"INSERT `resources` (id, somefield)
VALUES (20004,'Iñtërnâtiônàlizætiøn');");
You need to specify that it's 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');");
You probably need to select the correct culture and provide it to string.Format. You may want to 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 with regard to character set collation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.