c#客户端连接mysql数据库插入中文字符乱码解决方案(超简单)

时间:2023-01-08 07:31:32

引用mysql.data.dll

连接语句中加入charset=utf8就好了

实例:

MySqlConnection mySqlC = new MySqlConnection("user id=root;password=ebdoor0044;host=localhost;database=scbhsw;charset=utf8");
MySqlCommand msqlCmd;
string strSql;

strSql = "insert into cd_news (cid,title,author,addTime,content,pic,clickNum) values (1,'我的标题','winne','1999-09-09','current','fsafsaf',33)";

msqlCmd = new MySqlCommand(strSql, mySqlC);

mySqlC.Open();
msqlCmd.ExecuteNonQuery();
msqlCmd = new MySqlCommand("select * from cd_news", mySqlC);
MySqlDataAdapter msqlAd = new MySqlDataAdapter(msqlCmd);
DataSet ds=new DataSet();
msqlAd.Fill(ds,"cd_news");
dataGridView1.DataSource=ds.Tables[0].DefaultView;

mySqlC.Close();