使用C#在MySql中添加和检索特殊字符 [英] Adding and Retrieving special characters to/from MySql using C#

查看:320
本文介绍了使用C#在MySql中添加和检索特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



好​​的,我有以下问题。我有一个带有下拉列表的Winform,显示世界上的每个货币代码,如果用户选择它,则该框旁边会显示符号。符号是大多数货币的特殊字符。在框旁边显示货币时没有问题,因为它是硬编码的字符串。但是,从该列表中,用户将选择其国家货币和货币代码,并且符号将保存到数据库中。当我查看数据库时,一些特殊字符货币被保存为?....在整个应用程序的其余部分,符号不是硬编码的,因为它应该从数据库中检索符号,但现在它应该显示[符号] 100 ....它显示?例如100。



如何保存符号以便按照应有的方式检索符号?



希望你能理解这个问题并知道如何解决这个问题!



问候,

Chris

Hi everyone!

Ok, so I have the following problem. I have a Winform with a dropdown list displaying every currency code in the world and if a user selects it the symbol displays next to the box. The symbols is a special character for most of the currencies. When displaying the currency next to the box there is no problem as it is a hard coded string. However, from this list the user will select his country currency and the currency code and the symbol is saved to the database. When I look at the database some of the special character currencies are saved as "?".... Throughout the rest of the application the symbol is not hard coded as it should retrieve the symbol from the database, but now when it should show [Symbol]100 .... it shows ?100 for example.

How do I save the symbol so it would be retrieved the way it should?

Hope you understand the question and know how to fix this!

Regards,
Chris

推荐答案

要检查的第一件事是将符号存储到的列格式 - 它应该是NVARCHAR而不是VARCHAR,因为前者是Unicode和后者不是(假设您有MySQL 5或更高版本)



然后,将数据传输到列中:如果要连接字符串以形成SQL然后你需要在字符串常量前加上N来表示它已经是国家字符集数据:

The first thing to check is the column format you are storing the symbol into - it should be NVARCHAR not VARCHAR as the former is Unicode and the latter is not (assuming you have MySQL 5 or above)

Then, there is the transfer of data into the column: if you are concatenating strings to form your SQL statement then you need to prefix the string constant with "N" to indicate that it is already National Character Set data:
INSERT INTO MyTable (currency) VALUES (N'£')



如果你通过参数化查询来做(因为你肯定应该这样做)无论如何要避免SQL注入攻击)然后这是由系统自动处理的。



读出应该没问题:一旦你把它拿进来应该只是工作


If you do it via parameterized queries (as you definitely should be anyway to avoid SQL Injection Attacks) then that is handled by the system automatically.

Reading out should be no problem: once you have got it in it should "just work"


这篇关于使用C#在MySql中添加和检索特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆