SQL 如何在sql数据库中插入阿拉伯字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2881682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert arabic characters into sql database?
提问by pavanred
How can I insert arabic characters into sql database? I tried to insert arabic data into a table and the arabic characters in the insert script were inserted as '??????'
in the table.
如何在sql数据库中插入阿拉伯字符?我尝试将阿拉伯语数据插入表中,插入脚本中的阿拉伯语字符插入'??????'
表中。
I tried to directly paste the data into the table through sql management studio and the arabic characters was successfully and accurately inserted.
我尝试通过sql管理工作室直接将数据粘贴到表中,并且成功准确地插入了阿拉伯字符。
I looked around for resolutions for this problems and some threads suggested changing the datatype to nvarchar
instead of varchar
. I tried this as well but without any luck.
我四处寻找解决此问题的方法,一些线程建议将数据类型更改nvarchar
为varchar
. 我也试过这个,但没有任何运气。
How can we insert arabic characters into sql database?
我们如何将阿拉伯字符插入到sql数据库中?
回答by Guffa
For the field to be able to store unicode characters, you have to use the type nvarchar
(or other similar like ntext
, nchar
).
对于能够存储 unicode 字符的字段,您必须使用类型nvarchar
(或其他类似的类型,如ntext
, nchar
)。
To insert the unicode characters in the database you have to send the text as unicode by using a parameter type like nvarchar
/ SqlDbType.NVarChar
.
要在数据库中插入 unicode 字符,您必须使用nvarchar
/等参数类型将文本作为 unicode 发送SqlDbType.NVarChar
。
(For completeness: if you are creating SQL dynamically (against common advice), you put an N before a string literal to make it unicode. For example: insert into table (name) values (N'Pavan')
.)
(为了完整性:如果您正在动态创建 SQL(反对常见建议),您可以在字符串文字前放置一个 N 以使其成为 unicode。例如:insert into table (name) values (N'Pavan')
。)