采用的是 MySQL 5.5.15,一路设置都是默认的现有表
create table person(
id char(20) not null,
name char(50) not null,
primary key(id)
);使用 MySql.Data.dll  操作数据库public int ExecuteNonQuery(string sql)
{
            MySqlCommand command = new MySqlCommand(sql, m_conn);
            return command.ExecuteNonQuery();
}public DataTable ExecuteQuery(string sql)
 {
     DataTable dt = null;
     MySqlDataAdapter mda = null;
     try
     {
          mda = new MySqlDataAdapter(sql, m_conn);
          dt = new DataTable();
          mda.Fill(dt);
     }
     catch { }
     return dt;
}现在有 sql 语句   insert into person vaules ('12345678', '张三丰')  
当我在  mysql command line client 输入并执行完毕后,在mysql command line client下查询 select * from person 的结果能够显示中文
但是我如果在程序中调用 ExecuteNonQuery(sql) ,在mysql command line client下查询 select * from person 的结果,名字一列是乱码的
还有包含中文,我在程序调用  DataTable ExecuteQuery(string sql) 得到的结果也是乱码的mysql> select * from customer;
+----------+--------+
| id       | name   | 
+----------+--------+
| 12345678 | 张三丰 | 
+----------+--------+
1 row in set (0.00 sec)mysql> show variables like "char%";
+--------------------------+----------------------------------------------------
-----+
| Variable_name            | Value
     |
+--------------------------+----------------------------------------------------
-----+
| character_set_client     | latin1
     |
| character_set_connection | latin1
     |
| character_set_database   | latin1
     |
| character_set_filesystem | binary
     |
| character_set_results    | latin1
     |
| character_set_server     | latin1
     |
| character_set_system     | utf8
     |
| character_sets_dir       | C:\Program Files\MySQL\MySQL Server 5.5\share\chars
ets\ |
+--------------------------+----------------------------------------------------
-----+
8 rows in set (0.00 sec)
请教一下高手们应该怎么解决这个问题

解决方案 »

  1.   

    在C#查询前先执行一下SQL语句 set names 'gbk'; 试一下。如果没问题,则可以在连接字符串中设置连接的字符集。
    http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx
    MySQL 中文显示乱码
      

  2.   

    http://dev.mysql.com/doc/refman/5.1/zh/charset.html
      

  3.   

    执行语句前  set names gbk试试
      

  4.   

    我在public DataTable ExecuteQuery(string sql)的第一行增加了sql = "set names 'gbk'; " + sql;
    最后结果
    得到的名字还是 ????·á            set names 'gb2312' 也试了,效果一样
    调试的时候看了,在查询结果里面 DataRow name列的值就是乱码的
      

  5.   

     重新建表create table person(
    id char(20) not null,
    name char(50) not null,
    primary key(id)
    )DEFAULT CHARSET=gbk;然后set names gbk; 
      

  6.   


    mysql> show create table person;
    +----------+--------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------+
    | Table    | Create Table                                                  |
    +----------+--------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------+
    | customer | CREATE TABLE `person` (
      `id` char(30) NOT NULL,
      `name` char(30) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=gbk |
    +----------+--------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------+
    1 row in set (0.00 sec)现在的情况是,我在mysql command line client 下面输入
    inert into person values('1234','张三丰');
    在程序里面    select * from person; 查询出来的结果,即使没有在前面增加 set names gbk; 也是能够显示中文的但是用代码存入表的时候,比如
    sql = " set names bgk; inert into person values('12345','张三丰'); "
    表里面是乱码的,查询出来的也是乱码
      

  7.   

    我碰到的这个问题,在  sql="set names gbk; "+sql; 执行还是不行
    解决方法是string url = "Database=db;Data Source=localhost;User Id=user;Password=pwd;charset=gbk;";//在这里增加设定
    MySqlConnection m_conn = new MySqlConnection(url);