写了个程序,C#连接mysql,当我用mysqldatareader
读取记录的时候
reader.GetString(0);
获取的的数据是乱码。请问该怎么从mysqldatareader里面读取到中文呢?
在线等...
读取记录的时候
reader.GetString(0);
获取的的数据是乱码。请问该怎么从mysqldatareader里面读取到中文呢?
在线等...
解决方案 »
- VS2008 C#用manifest提升程序权限后,调试时还是不行,求解
- 在vs2005和vs2008中查看一个接口的所有实现的快捷键
- openfiledialog在屏幕上的显示位置啊?
- ★★ 窗体无反应 假死
- 谁在win2k3 sp1下成功装上vs2005 sp1了
- LSB算法分析
- 请教一个关于DbDataAdapter.Update 的问题。多谢!
- 简单问题,给个建议:这样的要求用什么样的方法处理比较好?还真是万事开头难啊!
- 如何手动添加xtragridView行?
- 菜鸟的难题(二)???
- ultraISO 多个ISO文件设置成U盘启动
- The underlying connection was closed:=========来者有分
com.ExecuteNonQuery();
//按照楼上的想法增加上面两行代码。
MySqlDataReader reader = cmd.ExecuteReader();
while(reader.read)
{
string str = reader.GetString(0);
//但是在这读到的还是乱码
}
charset=utf8;
MysqlConString = "Server =" + Ip + "; Database = " + DataBase + "; Uid = " + UserName + "; Pwd = " + PassWord + ";" + "charset= 'utf8'";
<add name="MySqlConnectionString" connectionString="Server=***;Database=***;User ID=***;Password=***;charset=utf8;" />我当初遇到问题时,这样就正确了。
我已经这样了。还是不可以。。哎我在网上查了下资料。
好像说是数据库编码问题。
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了。。
但是还是乱码
那位高手帮帮忙吧~~急死了。。
你用新的连接字符串后,把上面这话从导入的语句中去掉
再导入一次试试吧
幫頂 同意樓上的說法。。 數據庫的每個字段也要設為gbk 吧
去掉之后无法导入了。。格式不对
错误: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
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');
我的表语句代码:
string str = reader.GetString(0);
//但是在这读到的还是乱码
}索引0代表的是 int类型的 应该
int = reader.GetInt(0)吧
{
try
{
OdbcCommand com = new OdbcCommand("select names from gb2312", con);
com.ExecuteNonQuery();
//按照楼上的想法增加上面两行代码。
OdbcDataReader reader = cmd.ExecuteReader();
while(reader.read)
{
string str = reader.GetString(0);
//但是在这读到的还是乱码
}
}
如果还不行,把库转unicode类型还不行的话,说明老数据已乱码,折腾乱了,加新数据试
重装数据库,配置向导选择字符集utf8可以了。
刚才配置的是GBK和gb2312都不可以