C#連接mysql中文亂碼寫入資料庫"???" MySql.Data.dll

C# 使用 MySql.Data.dll 連結資料庫會有中文亂碼問題,主要是沒有連結字串內設定UTF-8。

只要將下列連結方式加入charset=utf8就可解決,C# 寫入mysql資料庫,就不會有亂碼如"????"的字符產生

將下行加入charset=utf8;

string myConnectionString = "server =192.168.xxx.xxx; Database=mydb;User Id=admin;Password=admin;charset=utf8;";

完整連結方式:

string myConnectionString = "server =192.168.xxx.xxx; Database=mydb;User Id=admin;Password=admin;charset=utf8;";
MySqlConnection DBConn = new MySqlConnection(myConnectionString);
DBConn.Open();
MySqlCommand cmd = new MySqlCommand("Insert into n5y91_hrsystem_schedule_test (xxx) values ('" + value +"')", DBConn);
try
{
 cmd.ExecuteNonQuery();
}
 catch (Exception ex)
{
 Console.WriteLine(ex.Message);
}

 

Posted in 電腦技巧

Find the last offers by SkyBet at www.bettingy.com BettingY.com Bonuses