I have a WinForms application which should be able to save (to db) and later display non latin characters. I know its something related to charset. My database, and all the tables have their default charset set to utf8. But this still doesn't save the non latin characters preserving their weirdness :).
After some googling and SO-ing I got to know it is something about SET NAMES 'utf8'
. But where do I apply this? A lot of similar threads on SO deals with php and they say one have to include SET NAMES 'utf8'
at the start of the query. But in my code there are hundreds of queries and tinkering each place is a mess.
If I need to configure MySQL for that, then can it be done programmatically? I hope there is some solution by setting this in connection string.
I tried this:
conString = "SERVER=localhost;DATABASE=hi;UID=root;PASSWORD=;Min Pool Size=0;Max Pool Size=200;SET NAMES 'utf8'";
which gives run time error saying connection string is incorrect format.
This answer says
The collation is only used for sorting and comparison, not for saving data.
I tried this which worked beautifully.
Edit: Let me make it complete.
.NET Connector manual says
"CharSet specifies the character set that should be used to encode all queries SENT TO THE SERVER. Resultsets are still returned in the character set of the result data."
Which means:
1, Setting
charset=utf8
is required only when saving the data to database, and not when retrieving since .NET connector gets retrieves it in the format its saved. I am not saying when reading data the connectionstring has to be modified to removecharset=utf8
,no. It works both ways. Just making it clear that only saving explicitly requirescharset=utf8
.2, Its not even paramount that your db has its default charset set to utf8. Even latin1 one would do if you have charset defined in connectionstring as utf8
I tested the above cases.