第一个网站 少了一个N
在线编辑器已经能够使用,下面开始实现新闻的‘增删改’,但是韩文页面在插入的时候是没有问题的,但是在插入完成之后,在从数据库中读出来,然后修改完成之后再写入数据库中,在读出来的话,上次修改的数据就成了乱码。好生郁闷,怎么办?总不能让韩文的页面不能修改吧。找bug。
- 首先新插入一条新闻,包含韩文和中文。
- 从数据库中读出来,数据是没什么问题的。
- 修改这条数据。将修改完成的数据添加到数据库中。
- 再从数据库中把修改完成的数据读出来,然后数据出错了。
接下来将update完的数据,直接从数据库中读出来,数据中出现乱码,显然错误出现在插入的环节,但是代码没有出现异常,说明c#的逻辑是没有什么问题的,那只能是sql语句的问题,考虑到MS一定会考虑到各国字符的编码问题(咱们能够想到的,MS应该都为我们想到了吧,微软的东东使用的舒服,但是不是free),于是google,得到:
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语句就是这样的:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?