Bug #55476 Problem writing and reading Euro from latin1 database
Submitted: 22 Jul 2010 10:54 Modified: 22 Jul 2010 12:23
Reporter: Stefano Sapienti Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.2.3 OS:Any
Assigned to: Vladislav Vaintroub CPU Architecture:Any

[22 Jul 2010 10:54] Stefano Sapienti
Description:
Hi, I'm connecting to a database created with latin1 charset. 
If I use a simple connection string without specific charset setting when I save the euro char a question char in inserted inside db (saw using Query browser).
If I use charset utf8 the euro char is correctly inserted but when I try to read it an "unreadable" char is returned by the datareader.
If before using the SELECT clause I issue a SET NAMES utf8 command the correct character is returned.

In mailing list I found a post saying that .NET Connector should automatically add  SET NAMES command but it seems not so.

I can't understand if it's this connected to bug #51927 or it Connector fails adding SET NAMES if charset is not the default.

How to repeat:
using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection("server=servername;database=dbname;username=username;pwd=password;")) {
using (MySql.Data.MySqlClient.MySqlCommand comm = conn.CreateCommand()) {
  comm.CommandText = "INSERT INTO TESTTABLE (TESTFIELD) VALUES ('€');
  conn.Open();
  comm.ExecuteNonQuery();
  conn.Close();

  // Look at data using Query Browser, a '?' char is present instead of euro.
  // if charset=utf8 there's the euro char

  comm.CommandText = "SELECT TESTFIELD FROM TESTTABLE WHERE TESTFIELD='€'";
  conn.Open();
  MySql.Data.MySqlClient.MySqlDataReader dr = comm.ExecuteReader();
  while (dr.Read()) {
    string test  = dr[0].ToString();
    // test is equal to '?'
    // if charset=utf8 test is equal to a square
    // if SET NAMES utf8; is added before select test is equal to €
  }
  conn.Close();
[22 Jul 2010 11:54] Susanne Ebrecht
Which OS do you use?
Windows or Linux?
[22 Jul 2010 12:22] Vladislav Vaintroub
Cannot reproduce using latest sources. 

Most likely it was fixed with the bug you mentioned. bug#51927. Anyway, below is the test case that passes with connStr containing either latin1 or utf8

           using (MySqlConnection c = new MySqlConnection(connStr))
            {
                c.Open();
                Console.WriteLine(c.ConnectionString);
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = c;
                cmd.CommandText = "CREATE TABLE Test(a char(10))";
                cmd.ExecuteNonQuery();

                cmd.CommandText = "INSERT INTO Test VALUES ('€')";
                cmd.ExecuteNonQuery();
                cmd.CommandText = "SELECT * FROM Test";
                string s = (string)cmd.ExecuteScalar();
                Assert.AreEqual(s, "€");
            }