views:

1033

answers:

1

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 look. 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');");
A: 

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');");
chris
No luck I'm afraid, still a line of garbage when the query is run.
Echilon
btw, did you save the .cs file that cointains the text 'Iñtërnâtiônàlizætiøn' with encoding? Check VS, Save as and click the arrow on the save command.
chris
It's saved as UTF8.
Echilon
Are you sure that the data in the DB is incorrect? Does the tool that you use for querying support UTF8?
chris
Yes, whether I view it in a GUI tool or through a browser, it's the same. I can view the correct data in both when I manually run the query using the same GUI tool.
Echilon
Any other suggestions?
Echilon
The only other thing I can do it just remove that line, but I wanted to demonstrate UTF8 really.
Echilon