1
votes

I am writing a C# program to copy all the data from a table in an MS SQL SERVER 2008r2 to a table in MySQL. I am using a DataGridView and fill it from the first table with this code:

this.tB_OstanTableAdapter.Fill(this.db.TB_Ostan);
foreach (db.TB_OstanRow row in db.TB_Ostan) {
  try {
    dgvData.Rows.Add(row.Name, row.parent_id.ToString(), " ");
  }
  catch {
    dgvData.Rows.Add(row.Name, "", " ");
  }
}

The sum of parent_ids is NULL. After this my DataGridView was successfully filled. Then I press the Save button and the data will be saved in the MySQL table.

KeyPress code:

for (int i = 0; i < dgvData.Rows.Count; i++) {
  try {
    locationsTableAdapter.Insert(dgvData.Rows[i].Cells[0].Value.ToString(),
    int.Parse(dgvData.Rows[i].Cells[1].Value.ToString()));
  }
  catch {
    try {
      locationsTableAdapter.InsertQueryOnlyName(dgvData.Rows[i].Cells[0].Value.ToString());
    }
    catch { }
  }
}
MessageBox.Show("Saved Successful.");

My database table in MySQL has the utf8_unicode_ci collation, but when I browse it in PHPMyAdmin, all strings show as "????".

My MySQL charset

+--------------------------+-----------------------------------------------+ | Variable_name | Value |

+--------------------------+-----------------------------------------------+

| character_set_client | utf8 |

| character_set_connection | utf8 |

| character_set_database | utf8 |

| character_set_filesystem | binary |

| character_set_results | utf8 |

| character_set_server | utf8 |

| character_set_system | utf8 |

| character_sets_dir | c:\wamp\bin\mysql\mysql5.5.20\share\charsets\ |

+--------------------------+-----------------------------------------------+

How can I fix this problem?

1

1 Answers

1
votes

[SOLVED]

I am using MySQL connector for .net and I set ; CharSet=utf8 in my connection string then everything was saved properly.