c# mysql 插入 和 查询,使用C#在MySql中添加和检索特殊字符

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

解决方案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'£')

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"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值