[英]Special character not saved in MS SQL
I have a small problem with this Czech character ů
. 这个捷克字符
ů
有点问题。 This little bugger won't save into my MS SQL table when the update is triggered from my C# code. 当从我的C#代码触发更新时,这个小bugger将无法保存到我的MS SQL表中。
If I manually add it in SQL Management Studio it is saved as it should, and it is also shown on the website as normal. 如果我在SQL Management Studio中手动添加它,它将按原样保存,并且它也会在网站上正常显示。
But the process of saving it from C# only ends in the character u
beeing saved in the table and not ů
. 但是从C#在保存的过程中只在字符结尾
u
beeing保存在桌上,而不是ů
。
My DB fields are of type nvarchar
and ntext
and the collation of the DB is French_CI_AS
. 我的数据库字段的类型为
nvarchar
和ntext
,数据库的排序规则为French_CI_AS
。 I'm using MSSQL 2008. 我正在使用MSSQL 2008。
Code: 码:
SqlCommand sqlcomLoggedIn = new SqlCommand("UPDATE Table SET id = 1, title = 'Title with ů' WHERE id = 1", sqlCon, sqlTrans);
int status = sqlcomLoggedIn.ExecuteNonQuery();
Any thoughts? 有什么想法吗?
The immediate problem is that you aren't using a unicode literal; 直接的问题是您没有使用Unicode文字。 it would have to be:
它必须是:
UPDATE Table SET id = 1, title = N'Title with ů' WHERE id = 1
the N
is important. N
很重要。
The much bigger issue is that you should be using parameters: 更大的问题是您应该使用参数:
int id = 1;
string title = "Title with ů";
// ^^ probably coming from C# parameters
using(var sqlcomLoggedIn = new SqlCommand(
"UPDATE Table SET title = @title WHERE id = @id", sqlCon, sqlTrans))
{
sqlcomLoggedIn.Parameters.AddWithValue("id", id);
sqlcomLoggedIn.Parameters.AddWithValue("title", title);
int status = sqlcomLoggedIn.ExecuteNonQuery();
}
then the problem goes away, you get to use cached query plans, and you avoid sql injection 然后问题就消失了,您可以使用缓存的查询计划, 并且可以避免sql注入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.