简体   繁体   English

MySQL ::连接器/净欧元符号

[英]MySQL :: Connector/Net euro sign

My MySQL database can store the euro symbol just fine (as I have tested with a native MySQL client (HeidiSQL)). 我的MySQL数据库可以很好地存储欧元符号(因为我已经使用本机MySQL客户端(HeidiSQL)进行了测试)。 But with the MySQL .NET connector my ASP.NET application is using I can't insert nor read it back from the database: I only get a ? 但是使用MySQL .NET连接器,我的ASP.NET应用程序正在使用我无法插入或从数据库中读取它:我只得到一个? character back. 回归。 What could be the possible cause? 可能的原因是什么?

I would suggest explicitly specifying the encoding in your connection string: 我建议在连接字符串中明确指定编码:

Server=localhost;Database=schema;Uid=foo;Pwd=bar; 服务器=本地主机;数据库=模式; UID = foo的; PWD =栏; CharSet=utf8 ; CharSet = utf8 ;

It usually resolves most encoding-related issues with MySQL's Connector/NET. 它通常可以解决MySQL的Connector / NET与编码相关的大多数问题。

I'd say that MySQL .NET connector sets some collation-related environment variables on connection; 我会说MySQL .NET连接器在连接时设置一些与整理相关的环境变量; compare output of these queries both on HeidiSQL and .NET: 在HeidiSQL和.NET上比较这些查询的输出:

SHOW VARIABLES LIKE "character_set_%"; 
SHOW VARIABLES LIKE "collation_%";

They all should contain utf8-something. 它们都应该包含utf8-something。 If not, you can alter them at runtime as in the manual . 如果没有,您可以在手册中更改它们, 如手册中所示

The following .Net command can encode these for your database, and then decode them into the original symbol... 以下.Net命令可以为您的数据库编码这些,然后将它们解码为原始符号...

System.Web.HttpUtility.HtmlEncode()

System.Web.HttpUtility.HtmlDecode()

Presumably you are inserting into the euro sign into a field that is typed as NVARCHAR or you wouldn't be able to insert and retrieve the euro sign correctly with other clients? 据推测,您是将欧元符号插入到键入NVARCHAR的字段中,否则您将无法正确插入和检索欧元符号与其他客户端?

Are you using the correct syntax in your SQL statements for inserting and retrieving Unicode data? 您是否在SQL语句中使用正确的语法来插入和检索Unicode数据?

To do this you must use the N specifier character before any string which is unicode or it will get silently converted to a single byte encoding and become an unreadable character eg 为此,您必须在任何unicode字符串之前使用N说明符字符,否则它将被静默转换为单字节编码并成为不可读的字符,例如

//Inserting Unicode data
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "INSERT INTO SOME_TABLE (someField) VALUES (N'@text')";
cmd.Parameters.Add("text", "Some Unicode Text");
cmd.Connection = conn;
cmd.ExecuteNonQuery();

//Selecting Unicode data
MySqlCommand select = new MySqlCommand();
select.CommandText = "SELECT * FROM SOME_TABLE WHERE someField=N'@text'";
cmd.Parameters.Add("text", "Some Unicode Text");
cmd.Connection = conn;
//Execute your query and process your Results...

I had a similar problem, trying to import data from Sybase 12.5 to MS SQL Server 2005. 我遇到了类似的问题,尝试将数据从Sybase 12.5导入到MS SQL Server 2005。

Basically MSSQL stores £ and euro signs fine in varchar if they're encoded right, which is great, but what I didn't realise is that Sybase was outputting the data as windows-1252 (western european) and not UTF-8 and this caused data like £ and the euro symbol to get translated by the import programme as '?'. 基本上,如果编码正确,MSSQL会将£和欧元标记在varchar中很好,这很好,但我没有意识到Sybase正在输出数据为windows-1252(西欧)而不是UTF-8导致数据如£和欧元符号被导入程序翻译为'?'。 Telling the import programme that the incoming data is encoded as 1252 fixed the issue. 告诉导入数据被编码为1252的导入程序修复了该问题。

If you encode the output from the DB as 1252 as you're reading it, you should get the right symbols out. 如果您在读取数据时将数据输出编码为1252,则应该输出正确的符号。

For example: 例如:

System.Text.Encoding enc = System.Text.Encoing.GetEncoding(1252);
MySqlDataReader msdr = command.ExecuteReader();
while(msdr.Read())
{
    ...
    string val = enc.GetString(enc.GetBytes(msddr.GetString(mssdr.GetOrdinal("varhcar_field"))));
    ...
}

Have you correctly set the charset in your output page ? 您是否在输出页面中正确设置了字符集? ie something like <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> or charset=iso-8859-15 but not charset=iso-8859-1 即像<META http-equiv="Content-Type" content="text/html; charset=UTF-8">charset=iso-8859-15但不是charset=iso-8859-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM