4

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

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

The really weird this is that this happens only with unicode strings such as Greek words and only when i send the query with parameters.

Ie. if my query as seen in C# is:

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

and i set the @somestring parameters value as "TESTING". This would work just fine.

If i try to set the value as unicode string "ΤΕΣΤΙΝΓ", the query executes fine with no errors but only saves half the characters in the database, ie. it only saves "ΤΕΣΤ".

On the other hand if i remove the parameters and adjust the query to be as:

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

the query again works just fine AND saves the whole word/sentence in the database.

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

Thanks

4

1 に答える 1

5

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

UTF-8 takes upto 3 bytes per character so you'd need length to be 21 not 7 for example to fit testing and varieties thereof

Saying that, I've not used c# to call MySQL (only SQL Server) but I'm sure this is the problem

于 2012-01-21T15:21:37.687 に答える