mantis bug管理系统的二次asp.net 接口开发的时候,碰到编码的问题,如上述标题。
此系统的功能比较好,开源的php+myql.
mysql 的 默认设置
file:my.ini
[mysql]
default-character-set=latin1
[mysqld]
default-character-set=latin1
mysql 数据库的编码细节查询都是latin1编码。
Mantis 系统的Mysql数据库链接
数据库编码方式:latin1
当前项目文档的编码方式: utf8
对中文字符串的更新操作和查询操作需要编码格式转换
转换方式:Formarter.GetEncodeToLatin|Formarter.GetEncodeFromLatin
更新sql语句之前使用set names latin1;
例如:
插入数据
sql = "set names latin1;insert into tableName( col1 ) values( '?col1')"
MySqlCommand comm = new MySqlCommand(sql, conn);
MySqlParameter pcol1 = new MySqlParameter("?col1", MySqlDbType.VarChar);
//编码转换
pcol1.Value = Formarter.GetEncodeToLatin(col1);
comm.Parameters.Add(pcol1);
//查询数据
sql = "set names latin1;select realName from mantis_user_table where id=" + reporterUserId;
comm = new MySqlCommand(sql, conn);
dr = comm.ExecuteReader();
if (dr.Read())
{
reporterRealName = dr["realName"].ToString();
//编码转换
reporterRealName = Formarter.GetEncodeFromLatin(reporterRealName);
}
dr.Close();
publicstaticstringGetEncodeToLatin(stringsrc)
...{
returnSystem.Text.Encoding.GetEncoding("latin1").GetString(System.Text.Encoding.Default.GetBytes(src));
}
publicstaticstringGetEncodeFromLatin(stringsrc)
...{
returnSystem.Text.Encoding.Default.GetString(System.Text.Encoding.GetEncoding("latin1").GetBytes(src));
}
页面的编码也要统一,尽可能使用System.Text.Encoding.Default获取默认的编码方式,而不是utf8方式
在数据库中编码改变时,sql语句的set names latin1也要相应的变化才能正常录入中文
分享到:
相关推荐
解决办法: 在my.ini里找到 sql-mode=”STRICT_TRANS_TABLES,NO_AUTO_Create_USER,NO_ENGINE_SUBSTITUTION” 把其中的STRICT_TRANS_TABLES,去掉, 或者把sql-mode=STRICT_TRANS_TABLES,NO_AUTO_Create_USER,NO_ENGINE_...
主要介绍了解决出现Incorrect integer value: '' for column 'id' at row 1的问题的相关资料,希望通过本文能帮助到大家,让大家遇到这样的问题及时的解决,需要的朋友可以参考下
罗集比较全的数据库乱码问题的解决方案,,讲明了原因和解决方法
linux ls find 时大于2G文件时出现Value too large for defined data type 系统本身支持大于2G文件但查询有问题的解决办法
MySql写入中文错误解决办法,对于导入中文, 数据老是报错"Data too long for column XX at row n
mysql5.0乱码的解决实例:用代码方式实现乱码的解决! data too long for column........
网页制作作业网页制作作业网页制作作业网页制作作业网页制作作业网页制作作业网页制作作业网页制作作业
Elsevier.Database Archiving.How to Keep Lots of data for a very Long Time.2009.pdf
Advanced Analytics with Spark: Patterns for Learning from Data at Scale by Sandy Ryza English | 12 Jun. 2017 | ASIN: B072KFWZ8S | 281 Pages | AZW3 | 1.81 MB In the second edition of this practical ...
Data Preparation for Data Mining Using SAS
for ground-based aerosol monitoring and imposes a stan- dardization for these measurements. The system’s auto- matic data acquisition, transmission, and processing fa- cilitates aerosol ...
Practical Data Mining for Business presents a user-friendly approach to data mining methods, covering the typical uses to which it is applied. The methodology is complemented by case studies to ...
Review, 'A must-read resource for anyone who is serious about embracing the opportunity of big data.', -- Craig Vaughan, Global Vice President at SAP, 'This book goes beyond data analytics 101....
UserBehavior是阿里巴巴提供的一个淘宝用户行为数据集,用于隐式反馈推荐问题的研究。 UserBehavior.csv 包含所有的用户行为数据 用户ID,商品ID,商品类目ID,行为类型,时间戳。 UserBehavior.csv 本数据集包含了...
Learn Docker "infrastructure as code" technology to define a system for performing standard but non-trivial data tasks on medium- to large-scale data sets, using Jupyter as the master controller. ...
Advanced Analytics with Spark Patterns for Learning from Data at Scale 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Review, 'A must-read resource for anyone who is serious about embracing the opportunity of big data.', -- Craig Vaughan, Global Vice President at SAP, 'This book goes beyond data analytics 101....