Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用内联查询在SQL Server中插入unicode字符_C#_Asp.net_Sql_Sql Server_Database - Fatal编程技术网

C# 如何使用内联查询在SQL Server中插入unicode字符

C# 如何使用内联查询在SQL Server中插入unicode字符,c#,asp.net,sql,sql-server,database,C#,Asp.net,Sql,Sql Server,Database,我在asp.net中使用内联查询,但问题是我现在不知道如何正确使用N后缀,请指导 cn.Open(); SqlCommand cmd = new SqlCommand("insert into newsactivity VALUES(@newstitle, @newsdetails ,@dated,@type)",cn); cmd.Parameters.AddWithValue("@newstitle",txtnewstitle.Text); cmd.Paramete

我在asp.net中使用内联查询,但问题是我现在不知道如何正确使用N后缀,请指导

  cn.Open();
    SqlCommand cmd = new SqlCommand("insert into newsactivity VALUES(@newstitle, @newsdetails ,@dated,@type)",cn);
    cmd.Parameters.AddWithValue("@newstitle",txtnewstitle.Text);
    cmd.Parameters.AddWithValue("@newsdetails",N'txtnewsdetails.Text');

你不需要。NET字符串已经是unicode。N只是告诉查询分析器N后面的字符串文字是Unicode

e、 g

只需确保
newsactivity
表中的
newsdailets
字段是
NVARCHAR(…)

编辑 真奇怪。AddWithValue可能未正确推断参数的类型。您可以显式指定它:

SqlParameter param = cmd.Parameters.AddWithValue("@newsdetails",txtnewsdetails.Text);
param.SqlDbType = SqlDbType.NVarChar;

显式指定类型和长度的另一个很好的原因是,如果在WHERE子句筛选器中使用参数。请看

这样说就足够了

cmd.Parameters.AddWithValue("@newsdetails", txtnewsdetails.Text);
编辑:如果失败,那么这应该可以工作:

cmd.Parameters.Add("@newsdetails", SqlDbType.NVarChar, 1024).Value = txtnewsdetails.Text;

在参数中使用
SqlDbType.NVarChar

当您使用参数时,将
SqlDbType
设置为
NVarChar
应该很好,尽管它的NVarChar已经。。。它是一个免费文本框,但保存后会显示?????当我直接在sql server中插入unicode时,sql server是如何接受它的
cmd.Parameters.Add("@newsdetails", SqlDbType.NVarChar, 1024).Value = txtnewsdetails.Text;