asp.net连接MYSQL的SQL语句中有汉字问题最简单、最终的解决方法

VS2010 中用asp.net开发的程序连接MYSQL5.6数据 ,

由于MYSQL采用UTF-8编码,而asp.net采用的是GB2312,导致在查询语句中 汉字时,即就根本查询不出相应的数据来。

而在MySQL Workbench 6.0 CE中执行SQL语又没有问题,

如:

select UserID,UserName,LoginAccount,LoginPWD,DeleteFlag,AddTime,AddUserID,RoleID  FROM t_user  where  DeleteFlag = '启用' and  trim(LOWER(LoginAccount))='admin' and trim(lower(LoginPWD))='admin'

由于SQL语句中带有汉字,所以,查询时返回结果总为0行。删除DeleteFlag = '启用'的条件后就可以返回数据。

网上查了很多,有说设置web.config中的编码为utf-8的 有说设置页面的编码为utf-8的,结果都不管用。

 

最终解决办法 :

在MYSQL的连接字符串中设置编码格式即可。

我的web.config设置如下:

<connectionStrings>
  <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
 </connectionStrings>
 <appSettings>
    <add key="MySQLConnString" value="server =localhost;port=3306;Charset=utf8; user id =yygh118114; password =yygh118114; database =yygh118114;"/>
  </appSettings>
 <system.web>
   <globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312"/>
  ...

最终问题得以解决。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用以下代码实现asp.net连接MySQL数据库: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using MySql.Data.MySqlClient; public class MySQLDBHelper { private static string connectionString = "server=localhost;user id=root;password=123456;database=test;Charset=utf8;"; public static DataTable ExecuteDataTable(string commandText, CommandType commandType, params MySqlParameter[] parameters) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand command = new MySqlCommand(commandText, connection)) { command.CommandType = commandType; if (parameters != null) { command.Parameters.AddRange(parameters); } MySqlDataAdapter adapter = new MySqlDataAdapter(command); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); return dataTable; } } } public static int ExecuteNonQuery(string commandText, CommandType commandType, params MySqlParameter[] parameters) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand command = new MySqlCommand(commandText, connection)) { command.CommandType = commandType; if (parameters != null) { command.Parameters.AddRange(parameters); } connection.Open(); int result = command.ExecuteNonQuery(); return result; } } } } ``` 其中,MySQLDBHelper是一个帮助类,提供了两个静态方法,一个用于执行SELECT语句并返回DataTable,另一个用于执行INSERT/UPDATE/DELETE等操作并返回受影响的行数。需要将connectionString变量替换为自己的MySQL连接字符串。使用示例: ``` MySqlParameter[] parameters = new MySqlParameter[] { new MySqlParameter("@name", "张三"), new MySqlParameter("@age", 20) }; string sql = "INSERT INTO student (name, age) VALUES (@name, @age)"; int result = MySQLDBHelper.ExecuteNonQuery(sql, CommandType.Text, parameters); if (result > 0) { Response.Write("添加成功!"); } else { Response.Write("添加失败!"); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xjzdr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值