在ASP.NET与MySQL数据库打交道的时候,由于配置的问题,会遇到自己写的方法在读取数据库中数据的时候,英文,数字可以正常通过,但是中文就无法通过,以登录为例(方法略),当输入英文用户名的时候可以正常登录,但输入中午的时候方法就会报错,解决方法如下:
1,在MySQL数据库安装目录下找到my.ini文件,把default-character-set的值修改为 default-character-set=gb2312(修改两处),保存,重新启动。
2,找到asp.net文件的web.config文件,在标签的后面添加
3,如果连接数据库的字符串写在web.config里面,则在连接字符串里面添加charset=gb2312(如:
4,重新编译。
有时还会遇到数据库里面显示的数据是中午,但读取到页面之后成了乱码,而且在写存储工程的时候,有中文出现,编译不会通过,我的解决方法是,我的解决方法是,把中文都转化为十六进制,然后读出来。代码如下
Mysql: drop procedure if exists proc_hm_holiday_top5; create procedure proc_hm_holiday_top5() begin select *,(select userinfo_name from hm_userinfo where userinfo_id=holiday_uid) as username, case when holiday_type=1 and holiday_typeTwo=1 then _utf8 0xE8AFB7E581872FE79785E5818720 when holiday_type=1 and holiday_typeTwo=2 then _utf8 0xE8AFB7E581872FE4BA8BE5818720 when holiday_type=1 and holiday_typeTwo=3 then _utf8 0xE8AFB7E581872FE4B8A7E5818720 when holiday_type=1 and holiday_typeTwo=4 then _utf8 0xE8AFB7E581872FE5A99AE5818720 when holiday_type=1 and holiday_typeTwo=5 then _utf8 0xE8AFB7E581872FE4BAA7E5818720 when holiday_type=1 and holiday_typeTwo=6 then _utf8 0xE8AFB7E581872FE5B9B4E5818720 when holiday_type=1 and holiday_typeTwo=7 then _utf8 0xE8AFB7E581872FE585B6E4BB9620 when holiday_type=2 then _utf8 0xE58AA0E78FAD when holiday_type=3 then _utf8 0xE587BAE5B7AE when holiday_type=4 then _utf8 0xE7A7BBE4BC91 end as holiday_class from hm_holiday where 1=1 order by holiday_addTime desc limit 5; end; |
public string GetHexFromChs(string s) { if ((s.Length % 2) != 0) { s += ” “;//空格 } System.Text.Encoding chs = System.Text.Encoding.GetEncoding(“utf-8”); byte[] bytes = chs.GetBytes(s); string str = “”; for (int i = 0; i < bytes.Length; i++) { str += string.Format(“{0:X}”, bytes[i]); } return str; } private void button1_Click(object sender, EventArgs e) { string shuru = textBox1.Text.Trim().ToString(); string str = GetHexFromChs(shuru); textBox2.Text = str.Trim().Trim(); } |
我们一直都在努力坚持原创.......请不要一声不吭,就悄悄拿走。
我原创,你原创,我们的内容世界才会更加精彩!
【所有原创内容版权均属TechTarget,欢迎大家转发分享。但未经授权,严禁任何媒体(平面媒体、网络媒体、自媒体等)以及微信公众号复制、转载、摘编或以其他方式进行使用。】
微信公众号

TechTarget
官方微博

TechTarget中国
作者
相关推荐
-
2017年3月数据库流行度排行榜 Oracle卫冕之路困难重重
时隔一个月,数据库市场经过一轮“洗牌”,旧的市场格局是否会被打破,曾经占巨大市场份额的企业是否可能失去优势?
-
2017年2月数据库流行度排行榜 攻城容易守城难
2016年下半年,数据库排行榜的前二十名似乎都“固守阵地”,在排名上没有太大的变动。随着2017年的悄然而至,数据库的排名情况是否会有新的看点?
-
MySQL管理特性:让企业适合交易平台
当Alexander Culiniac和他的同事在TickTrade系统公司建立一个基于云的交易平台时,面临一些基本的约束。那就是,系统必须在云上工作良好并且经济实用。
-
数据库和数据仓库的区别在哪儿?
目前,大部分数据仓库还是用数据库进行管理。数据库是整个数据仓库环境的核心,是数据存放的地方和提供对数据检索的支持。