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也要相应的变化才能正常录入中文
分享到:
相关推荐
### "Value too large for defined data type" 解决办法 在Linux环境下进行文件操作时,可能会遇到一个名为“Value too large for defined data type”的错误。这一问题通常发生在使用`ls`或`find`命令来查看大于2...
但是,当我们尝试导入中文字符时,总是报错“Data too long for column XX at row n”,让人很头疼。下面我们来详细解释这个问题和解决方案: 问题描述 在 MySQL 中,当我们尝试导入中文字符时,总是报错“Data ...
"data too long for column"错误提示则表明尝试存储的数据超过了列的最大长度,这与字符集编码有关,也可能导致乱码。下面我们将深入探讨如何识别和解决这些问题。 首先,我们要理解MySQL中的字符集。字符集定义了...
1. HTML基础:HTML(HyperText Markup Language)是网页内容的基本结构语言,用于定义网页的元素和结构。在Dreamweaver中,可以直接通过可视化编辑器插入HTML标签,如标题(h1-h6)、段落(p)、链接(a)、图像...
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 ...
Oracle Data Provider for .NET 2.0 10.2.0.2.20 Oracle Database Extensions for .NET 1.x 10.2.0.2.20 — 仅用于升级 Oracle Database Extensions for .NET 2.0 10.2.0.2.20 — 仅用于升级 Oracle ...
Range Loss for Deep Face Recognition with Long-Tailed Training Data,ICCV 2017,商汤科技 本文的贡献: - 研究长尾分布对人脸 CNNs 模型训练的影响,并想办法缓解 - 开发出新loss —— Range loss ...
"Data Analytics with Hadoop: An Introduction for Data Scientists" ISBN: 1491913703 | 2016 | PDF | 288 pages | 7 MB Ready to use statistical and machine-learning techniques across large data sets? ...
7th Edition balances technical and practical everyday aspects of data communications for future business managers, computer programmers, and system designers needing a thorough understanding of basic...
With this book, you’ll gain a clear understanding of this discipline for discovering natural laws in the structure of data. Along the way, you’ll learn how to use the versatile R programming ...
Right now Alexey works as a data scientist at Searchmetrics, wherein his day-to-day job he actively uses Java and Python for data cleaning, data analysis, and modeling. His areas of expertise are ...
MySql.Data.dll 6.4.4 for .net 4.0
on column-oriented stores, we have developed a new specialized analytic data engine tightly-coupled with the Tableau data visualization system. The Tableau Data Engine ships as an integral part of ...