asp.net使用Mysql乱码处理
2011-10-26 12:49
375 查看
在asp.net与mysql数据库打交道的时候,由于配置的问题,会遇到自己写的方法在读取数据库中数据的时候,英文,数字可以正常通过,但是中文就无法通过,以登录为例(方法略),当输入英文用户名的时候可以正常登录,但输入中午的时候方法就会报错,解决方法如下:
1,在 mysql数据库安装目录下找到my.ini文件,把default-character-set的值修改为 default-character-set=gb2312(修改两处),保存,重新启动。
2,找到asp.net文件的web.config文件,在</httpModules>标签的后面添 加<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
3,如果连接数据库的字符串写在web.config里面,则在连接字符串里面添加charset=gb2312(如:<add key="test" value="server=localhost;database=dbtest;uid=root;pwd=root; 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;
C#代码(将中文转化为十六进制)
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();
}
1,在 mysql数据库安装目录下找到my.ini文件,把default-character-set的值修改为 default-character-set=gb2312(修改两处),保存,重新启动。
2,找到asp.net文件的web.config文件,在</httpModules>标签的后面添 加<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
3,如果连接数据库的字符串写在web.config里面,则在连接字符串里面添加charset=gb2312(如:<add key="test" value="server=localhost;database=dbtest;uid=root;pwd=root; 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;
C#代码(将中文转化为十六进制)
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();
}
相关文章推荐
- asp.net使用Mysql乱码处理
- asp.net使用Mysql乱码处理
- asp.net使用Mysql乱码处理
- Asp.net使用MySQL 乱码处理的破解方案
- asp.net使用Mysql乱码处理
- asp.net使用Mysql乱码处理
- asp.net使用Mysql乱码处理
- asp.net使用Mysql乱码处理
- asp.netx写入Mysql乱码的处理
- asp.net通过MySql.Data.DLL方式使用mysql
- [置顶] Ajax程序:处理异步调用中的异常(使用Asp.Net Ajax内建的异常处理方法)
- 使用ASP.NET一般处理程序或WebService返回JSON的实现代码
- 【ABP开发】:asp.net core 中使用mysql
- linux系统下基于mono部署asp.net,使用ef6与mysql出现的问题【索引】
- ASP.NET实现下载功能的中文文件名乱码处理
- docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
- ASP.NET AJAX使用UpdatePanel控件+Response.Write()报错 处理方法
- asp.net MVC 过滤器使用案例:统一处理异常顺道精简代码
- 使用 ASP.NET 一般处理程序或 WebService 返回 JSON
- 译:怎样使用ASP.NET,MSMQ及WINDOWS SERVICE进行异步编程来处理长进程