我用 navicat 在管理数据库,之前没有设置 编码的时候 navicat 表中的中文都是????? 后来在 连接属性-高级-(去掉)MySql字符集(上面的编码是utf-8) 中文就可以显示了。
但是我的asp.net WEB 程序连接MySql获取到的数据 ,中文还是乱码!
网上查询了下,说在 连接数据库的地方 加入 Charset=utf8
例如:
server=localhost;database=b2bdata;uid=root;pwd=123456;Charset=utf8
测试了以下依然是乱码 ,换成gb2312 也不行。。
后来又在网上看到一篇,在查询数据的地方加入 set names utf8
例如:
- C# code
public static DataSet Query(string SQLString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); //MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); MySqlDataAdapter command = new MySqlDataAdapter("set names utf8", connection); command.SelectCommand.CommandText=SQLString; command.Fill(ds, "ds"); } catch (MySqlException ex) { throw new Exception(ex.Message); } return ds; } }
测试了以下 不管是utf8 还是 gb2312 都是乱码。不知道什么原因!!!
那位大侠帮帮忙啊!!!
------解决方案--------------------------------------------------------
try
{
connection.Open();
MySqlDataAdapter command = new MySqlDataAdapter("set names gb2312", connection);
command.ExcuteNonQuery();
......
}
试试看吧
------解决方案--------------------------------------------------------
public static MySQLDataReader GetReaderMySql(string sql)
{
MySQLConnection conn = new MySQLDriverCS.MySQLConnection(connStringMySql);
try
{
conn.Open();
MySQLCommand cmd = new MySQLCommand("set names gb2312", conn);
cmd.ExecuteNonQuery();
cmd = new MySQLDriverCS.MySQLCommand(sql, conn);
return (MySQLDriverCS.MySQLDataReader)cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
System.Console.Write(e.Message);
throw new System.ArgumentOutOfRangeException("MySql数据库连接失败", e);
}
}
这个是我以前用过的asp连MySQL的DBHelper