第一个网站 少了一个N

在线编辑器已经能够使用,下面开始实现新闻的‘增删改’,但是韩文页面在插入的时候是没有问题的,但是在插入完成之后,在从数据库中读出来,然后修改完成之后再写入数据库中,在读出来的话,上次修改的数据就成了乱码。好生郁闷,怎么办?总不能让韩文的页面不能修改吧。找bug。

  • 首先新插入一条新闻,包含韩文和中文。
  • 从数据库中读出来,数据是没什么问题的。
  • 修改这条数据。将修改完成的数据添加到数据库中。
  • 再从数据库中把修改完成的数据读出来,然后数据出错了。

接下来将update完的数据,直接从数据库中读出来,数据中出现乱码,显然错误出现在插入的环节,但是代码没有出现异常,说明c#的逻辑是没有什么问题的,那只能是sql语句的问题,考虑到MS一定会考虑到各国字符的编码问题(咱们能够想到的,MS应该都为我们想到了吧,微软的东东使用的舒服,但是不是free),于是google,得到:

http://stackoverflow.com/

Would you tell me, please, when should I use N prefix before string in Transact-SQL query? I have started to work with a database where I don't get any results using query like this

SELECT * FROM a_table WHERE a_field LIKE '%а_pattern%'

until I change pattern to N'%а_pattern%'. I never had to add this prefix in the past, so I am curious. a_field is defined as nvarchar(255), but I think the reason is something else.

Thank you!

The following articles have some good information on the question. The short answer is just that there's a type mismatch between the unicode column and non-unicode string literal you're using. From the KB article, it looks like omitting the N prefix might still work in some cases, but it would depend on the code page and collation settings of the database. That might explain the change in behavior, if you were previously having success with the no-prefix method.

http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.htmlhttp://support.microsoft.com/default.aspx/kb/239530

其实上面的意思就是对于unicode的更新的话,需要使用N前缀。sql语句就是这样的:

posted @ 2010-06-17 16:51  qiang.xu  阅读(298)  评论(0编辑  收藏  举报