HTML符号在SQL Server数据库中显示为问号 [英] HTML Symbols are being displayed as a question mark in SQL Server Database

查看:483
本文介绍了HTML符号在SQL Server数据库中显示为问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我注意到,诸如:★的html符号正在我的数据库中显示为问号。



我使用varchar作为类型,我使用的数据库是microsoft sql 2008。



有没有人知道这个问题的解决方法吗?

解决方案

您需要使用 NVARCHAR 数据类型对于您的列,VARCHAR数据类型只能用于非Unicode字符。如果你要在数据类型中存储unicode字符,你应该使用NVARCHAR数据类型,并且在向列中插入数据时使用 N 前缀告诉sql服务器将有一些unicode字符在传递的字符串。



使用VARCHAR DataType

  CREATE TABLE #Temp(Column1 VARCHAR(100))
INSERT INTO #Temp VALUES('★')
SELECT * FROM #Temp

结果 $ b

 ╔═════ ═════╗
║Column1║
╠════════
║? ║
╚════════

使用NVARCHAR DataType

  CREATE TABLE #Tempn(Column1 NVARCHAR(100))
INSERT INTO #Tempn VALUES N'★') - < - Unicode字符的前缀
SELECT * FROM #Tempn

结果

 ╔═══════
║Column1║
╠═══════
║★║
╚════════


Today i noticed that html symbols such as: ★ are being displayed in my database as a question mark.

I'm using varchar as type and the database i am using is microsoft sql 2008.

Does anyone know a fix for this?

解决方案

You need to use NVARCHAR datatype for your column, VARCHAR datatype can only be use for non-unicode character.

if you are storing unicode characters in your datatype you should use NVARCHAR datatypes and when inserting Data into your Column use the N prefix telling sql server there will be some unicode characters in the passed string.

With VARCHAR DataType

CREATE TABLE #Temp (Column1 VARCHAR(100))
INSERT INTO #Temp VALUES('★')
SELECT * FROM #Temp

Result

╔═════════╗
║ Column1 ║
╠═════════╣
║ ?       ║
╚═════════╝

With NVARCHAR DataType

CREATE TABLE  #Tempn (Column1 NVARCHAR(100) )
INSERT INTO #Tempn VALUES(N'★')        --<-- N prefix for Unicode Characters
SELECT * FROM #Tempn

Result

╔═════════╗
║ Column1 ║
╠═════════╣
║ ★       ║
╚═════════╝

这篇关于HTML符号在SQL Server数据库中显示为问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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