【问题标题】:Unicode in SQL Server 2012 ExpressSQL Server 2012 Express 中的 Unicode
【发布时间】:2012-07-05 10:24:56
【问题描述】:

我正在使用 c# 程序将一些 unicode 字符(例如中文字符)插入 SQL Server 2012 数据库。

我要插入的列是 nvarchar,但我仍然得到 ???????而不是正确的字符。我正在从 twitter 获取推文并将其插入数据库。

我正在使用 SQL Server 2012 Express。我必须安装任何语言包或更改设置吗?我对 SQL Server 不是很熟悉。

这就是我使用 c# 将代码插入数据库的方式:

String command4 = "INSERT INTO  tweets ([username], [createdDate], [tweet]) Values ('" + item.user.screen_name + "' , '" + item.created_at + "' ,N'" + escapedString + "')";

SqlCommand insertTweet = new SqlCommand(command4, connectionstring);

insertTweet.ExecuteNonQuery();

谢谢!

【问题讨论】:

  • 您不再需要任何东西 - SQL Server 完全能够处理 Unicode。请告诉我们如何从 C# 插入数据
  • @marc_s 抱歉,这就是我从 c# 插入数据的方式: String command4 = "INSERT INTO tweets ([username], [createdDate], [tweet]) Values ('" + item.user .screen_name + "' , '" + item.created_at + "' , '" + escapedString + "')"; SqlCommand insertTweet = new SqlCommand(command4, connectionstring); insertTweet.ExecuteNonQuery();
  • 同时显示你是如何检索显示 ?????????
  • @CodeByMoonlight 我执行语句 Select * from tweets 以在 sql express 中执行,然后能够看到表

标签: sql-server database


【解决方案1】:

将字符作为 SQL 字符串传递时,始终以“N”开头:

N'Unicode'

而不仅仅是

'Unicode'

或者在你的情况下:

@"INSERT INTO tweets ([username], [createdDate], [tweet]) 
Values (N'" + item.user.screen_name + "' , '" + item.created_at + "' , N'" + escapedString + "')"

顺便说一句,你应该use parameters。它可以一次解决许多问题(例如您拥有的问题、日期和数字格式、安全性......)

【讨论】:

  • 参数如:String username = item.user.screen_name ?
  • 不,像 SqlParameter 这样的参数(文档有帮助)。然后将值放入 SqlParameter 实例并在代码中按名称引用它。
  • 或者更好:使用参数而不是连接你的 SQL 语句!
  • @mar_s 你能给我一个关于参数部分的简短例子吗?谢谢!
  • 波斯语(波斯语)单词也有同样的问题。在它们前面加上 N 解决了​​这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
相关资源
最近更新 更多