写了个程序,C#连接mysql,当我用mysqldatareader
读取记录的时候
reader.GetString(0);
获取的的数据是乱码。请问该怎么从mysqldatareader里面读取到中文呢?
在线等...

解决方案 »

  1.   

    MySQLCommand com = new MySQLCommand("set names gb2312", conn);
      

  2.   

    MySqlCommand com = new MySqlCommand("set names gb2312", con);
    com.ExecuteNonQuery();
    //按照楼上的想法增加上面两行代码。
    MySqlDataReader reader = cmd.ExecuteReader();
    while(reader.read)
    {
    string str = reader.GetString(0);
    //但是在这读到的还是乱码
    }
      

  3.   

    写错了点。。while(reader.read())
      

  4.   

    数据库连接字符串里加上这一段
    charset=utf8;
      

  5.   


    MysqlConString = "Server =" + Ip + "; Database = " + DataBase + "; Uid = " + UserName + "; Pwd = " + PassWord + ";" + "charset= 'utf8'";
      

  6.   

    utf8不要加单引号,直接写charset=utf8
      

  7.   


    <add name="MySqlConnectionString" connectionString="Server=***;Database=***;User ID=***;Password=***;charset=utf8;" />我当初遇到问题时,这样就正确了。
      

  8.   


    我已经这样了。还是不可以。。哎我在网上查了下资料。
    好像说是数据库编码问题。
    http://blog.chinaunix.net/u2/77727/showart_2106432.html
    但是看起来怪麻烦的。也不懂怎么设置。。
    不知道是不是我导入数据有问题。。
    反正我导入的时候是通过脚本导入的。DROP TABLE IF EXISTS `tag`;
    CREATE TABLE `tag` (
      `tagid` int(9) unsigned NOT NULL AUTO_INCREMENT,
      `bigclassid` int(9) NOT NULL,
      `Tagname` varchar(100) DEFAULT NULL,
      `dir` varchar(255) DEFAULT NULL,
      `classid` varchar(255) DEFAULT NULL,
      `brandid` varchar(255) DEFAULT NULL,
      `tagurl` text,
      `taginfotxt` text,
      `taghtml` text,
      `orderby` int(9) DEFAULT NULL,
      PRIMARY KEY (`tagid`)
    ) ENGINE=MyISAM AUTO_INCREMENT=295 DEFAULT CHARSET=utf8;-- ----------------------------
    -- Records of tag
    -- ----------------------------
    INSERT INTO tag VALUES ('265', '100', '冰箱_两门冰箱_E智系列冰箱', '100\\265', null, null, null, null, null, '301');
    INSERT INTO tag VALUES ('266', '100', '冰箱_两门冰箱_E智触控系列冰箱', '100\\266', null, null, null, null, null, '303');
    INSERT INTO tag VALUES ('267', '100', '厨房电器_吸油烟机_中式吸油烟机_风翼系列吸油烟机', '100\\267', null, null, null, null, null, '302');
    INSERT INTO tag VALUES ('268', '100', '冰箱_三门冰箱_零度生物保鲜冰箱', '100\\268', null, null, null, null, null, '304');
    INSERT INTO tag VALUES ('269', '100', '热水器_E控家_E控家系列热水器', '100\\269', null, null, null, null, null, '307');
    INSERT INTO tag VALUES ('270', '100', '冰箱_三门冰箱_天弓系列冰箱', '100\\270', null, null, null, null, null, '305');
    INSERT INTO tag VALUES ('271', '100', '西门子众享系列热水器', '100\\271', null, null, null, null, null, '319');
    INSERT INTO tag VALUES ('272', '100', '西门子S系列小尺寸燃气灶', '100\\272', null, null, null, null, null, '309');
    INSERT INTO tag VALUES ('273', '100', '洗衣机_滚筒洗衣干衣机_天赋系列洗衣干衣机', '100\\273', null, null, null, null, null, '306');
    导入后,用mysqldatareader读取记录就会读到乱码
    连接字符串我已经设置为utf8了。。
    但是还是乱码
    那位高手帮帮忙吧~~急死了。。
      

  9.   

    DEFAULT CHARSET=utf8;
    你用新的连接字符串后,把上面这话从导入的语句中去掉
    再导入一次试试吧
      

  10.   


    幫頂 同意樓上的說法。。 數據庫的每個字段也要設為gbk 吧
      

  11.   


    去掉之后无法导入了。。格式不对
    错误:INSERT INTO tag VALUES ('265', '100', '冰箱_两门冰箱_E智系列冰箱', '100\\265', null, null, null, null, null, '301');
    [Err] 1366 - Incorrect string value: '\xE5\x86\xB0\xE7\xAE\xB1...' for column 'Tagname' at row 1
      

  12.   

    我的程序代码:string MysqlConString = "Server =127.0.0.1; Database = ***; Uid = root; Pwd = 123;charset= utf8";
    using (MySqlConnection con = new MySqlConnection(MysqlConString))
     {
      try
     {
    MySqlCommand com = new MySqlCommand("set names gb2312", con);
    com.ExecuteNonQuery();
    //按照楼上的想法增加上面两行代码。
    MySqlDataReader reader = cmd.ExecuteReader();
    while(reader.read)
    {
    string str = reader.GetString(0);
    //但是在这读到的还是乱码
    }
    }
    //....
    DROP TABLE IF EXISTS `tag`;
    CREATE TABLE `tag` (
      `tagid` int(9) unsigned NOT NULL AUTO_INCREMENT,
      `bigclassid` int(9) NOT NULL,
      `Tagname` varchar(100) DEFAULT NULL,
      `dir` varchar(255) DEFAULT NULL,
      `classid` varchar(255) DEFAULT NULL,
      `brandid` varchar(255) DEFAULT NULL,
      `tagurl` text,
      `taginfotxt` text,
      `taghtml` text,
      `orderby` int(9) DEFAULT NULL,
      PRIMARY KEY (`tagid`)
    ) ENGINE=MyISAM AUTO_INCREMENT=295 DEFAULT CHARSET=gb2312;-- ----------------------------
    -- Records of tag
    -- ----------------------------
    INSERT INTO tag VALUES ('265', '100', '冰箱_两门冰箱_E智系列冰箱', '100\\265', null, null, null, null, null, '301');
    INSERT INTO tag VALUES ('266', '100', '冰箱_两门冰箱_E智触控系列冰箱', '100\\266', null, null, null, null, null, '303');
    INSERT INTO tag VALUES ('267', '100', '厨房电器_吸油烟机_中式吸油烟机_风翼系列吸油烟机', '100\\267', null, null, null, null, null, '302');
    INSERT INTO tag VALUES ('268', '100', '冰箱_三门冰箱_零度生物保鲜冰箱', '100\\268', null, null, null, null, null, '304');
    INSERT INTO tag VALUES ('269', '100', '热水器_E控家_E控家系列热水器', '100\\269', null, null, null, null, null, '307');
    INSERT INTO tag VALUES ('270', '100', '冰箱_三门冰箱_天弓系列冰箱', '100\\270', null, null, null, null, null, '305');
    我的表语句代码:
      

  13.   

    上面代码全部是utf8也不可以!
      

  14.   

     
    string str = reader.GetString(0);
    //但是在这读到的还是乱码
    }索引0代表的是 int类型的 应该
    int = reader.GetInt(0)吧
      

  15.   

    int str = reader.GetInt(0)
      

  16.   

    using (OdbcConnection con=new OdbcConnection("DSN=mysql"))
     {
      try
     {
    OdbcCommand com = new OdbcCommand("select names from gb2312", con);
    com.ExecuteNonQuery();
    //按照楼上的想法增加上面两行代码。
    OdbcDataReader reader = cmd.ExecuteReader();
    while(reader.read)
    {
    string str = reader.GetString(0);
    //但是在这读到的还是乱码
    }
    }
      

  17.   

    重装数据库,配置向导选择字符集utf8还是不可以,崩溃了。。
      

  18.   

    连接字符用加上charset属性一般就解决问题了.
    如果还不行,把库转unicode类型还不行的话,说明老数据已乱码,折腾乱了,加新数据试
      

  19.   

    依稀记得mysql 数据库内的数据类型和读取数据时的数据类型都要设置才能行
      

  20.   


    重装数据库,配置向导选择字符集utf8可以了。
    刚才配置的是GBK和gb2312都不可以