简体   繁体   English

特殊字符未保存在MS SQL中

[英]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 . 我的数据库字段的类型为nvarcharntext ,数据库的排序规则为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.

相关问题 MS SQL Server 2008中的特殊字符插入 - Special character insertion in MS SQL Server 2008 搜索MS Access中包含特殊字符的字符串 - Search a string that contains special character in MS Access 将包含特殊字符&#39;的字符串插入sql db - insert into sql db a string that contain special character ' Windows 服务使用 SeriLog 没有日志保存到 MS SQL 服务器 - Windows Service Using SeriLog No Logs Saved To MS SQL Server 带参数的SQL数据库的gridview中不显示特殊字符 - Special Character does not displays in gridview from SQL database with Parameter 从sql查询填充下拉列表时如何转义特殊字符 - How to unescape special character when populating dropdownlist from a sql query 从C#将特殊字符插入SQL Server 2008数据库 - insert special character into SQL Server 2008 database from c# 如何在将已保存的文件从MS SQL Server2008恢复到本地驱动器时获取文件扩展名 - How to get File extension while retriving saved Files from MS SQL Server2008 to local drive Dapper for MySQL查询 <T> (字符串sql)sql具有自定义变量(@rowNum)包含特殊字符@ - Dapper For MySQL Query<T>(string sql) sql has a custom variables(@rowNum) contains a special character @ Nunit:uri中的特殊字符 - Nunit: Special character in uri
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM