I haven't seen something similar here, but if there is sorry for the duplicate post. Here is my setup>> I use an "outdated" Windows 7 machine with Visual Studio 2010 on it. Please bear with me.
The C# 2010 Express has worked so far so good. Recently I've been working with a MySQL database which is hosted on a WAMP (or XAMP or easyPHP) server. I am connecting the C# application to the MySQL database manually. Now, I am having a problem with a software I'm developing for a Bulgarian project (so it has to support Cyrillic characters).
Here is the code>>
private MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(); //connection
private MySql.Data.MySqlClient.MySqlDataAdapter data = new MySql.Data.MySqlClient.MySqlDataAdapter(); //adapter
connection.ConnectionString ="server=localhost;"+ "database=xxxxx;"+ "uid=root;"+ "password=;"; //connection string
try
{
connection.Open(); //opening the connection
MySql.Data.MySqlClient.MySqlCommand command = connection.CreateCommand(); //creating a query
command.CommandText = "SELECT * FROM data"; //initial query for listing the table
data.SelectCommand = command; //executing the query
DataSet dataset = new DataSet(); //C# procedures, creating a dataset for gridview
data.Fill(dataset, "sample_data"); //filling the dataset
dataGridView1.DataSource = dataset; //setting up the dataset towards the gridview
dataGridView1.DataMember = "sample_data"; //the data that are going to be sent...
}
And thus, I fill the dataGridView1 with the data from the database. And it is all good, the Cyrillic characters that have been filled up from before are looking good.
Now for the problem>>
If I want to send data into the database I write something like:
string comm1 = "INSERT INTO data (хххх, хххх) VALUES ('Abc', 'Абв')";
command.CommandText = comm1;
command.ExecuteNonQuery();
The first (latin) characters turn out to be fine, but the latter are not. In fact the database is filled up with question-mark signs (?).
What I've tried so far:
string commm2 = commm1.Replace("Дра", "\u0414 \u0440 \u0430");
. It doesn't work.I've converted the string
byte[] comm22 = Convert.FromBase64String(commm1); string commm3 = Encoding.UTF8.GetString(comm22);
. It doesn't work.
i had the same problem with Arabic strings in my php application ,i managed to solve it by executing the following query before sending or receiving data after making sure of tables and database collation setting set to utf8
mysqli_set_charset('utf8',$this->connection);
mysqli_query( $this->connection, 'SET NAMES "utf8" COLLATE "utf8_general_ci"' );
the problem is now solved but utf8 works for arabic , no need to change anything but executing the SET NAMES query before the (insert/select) query
hope it helps