Page 1 of 1

YIPES! Insert into TEXT column fails when using UTF-8 encoding

Posted: Fri 13 May 2005 20:02
by gwerner
Using MySQL Direct 2.80.5.0
MySQL 4.1.11-nt via TCP/IP

Table defined:
testID int NOT NULL - autoincrement
textfield text NOT NULL - utf8

The following code successfully runs, but fails to store anything after the first special character (Ñ):

Code: Select all

CoreLab.MySql.MySqlConnection conn = new MySqlConnection(connectionstring);
conn.Open();
string s = "UNA PEQUEÑA CONTRIBUCION";
using (MySqlCommand cmd = conn.CreateCommand())
{
	cmd.CommandType = CommandType.Text;
	cmd.CommandText = "insert into test (textfield) values (@textfield)";
	cmd.Parameters.Add("@textfield", s);
	cmd.ExecuteNonQuery();
}
conn.Close();
conn.Dispose();

Partial answer

Posted: Fri 13 May 2005 20:39
by gwerner
Okay, found out that I need to add conn.Unicode = true before the conn.Open command. Now I have the following question:

What is the easiest way to determine the version of MySQL so that I can determine if Unicode is supported (or is this detected by MySQLNet)? In other words, can I just always set Unicode=true?

One more thing

Posted: Fri 13 May 2005 20:50
by gwerner
One last note. Even if Unicode is set to false on the connection, the text should still be input. It should just replace all the unicode chars with ?s. That's what MySQL does itself. Right now it just truncates it.

Re: Partial answer

Posted: Mon 16 May 2005 07:22
by Serious
gwerner wrote:What is the easiest way to determine the version of MySQL
You can get MySQL Server version with MySqlConnection.ServerVersion property. See example in MySQLDirect .NET help.
gwerner wrote:In other words, can I just always set Unicode=true?
MySQLDirect .NET does not use unicode with servers that do not support it, so you can set Unicode=true safely.
gwerner wrote:One last note. Even if Unicode is set to false on the connection, the text should still be input. It should just replace all the unicode chars with ?s. That's what MySQL does itself. Right now it just truncates it.
This is default behavior of the System.Text.Encoding class.