0

我在 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');
4

3 回答 3

4

应该说够了

cmd.Parameters.AddWithValue("@newsdetails", txtnewsdetails.Text);

编辑:如果失败,那么这应该工作:

cmd.Parameters.Add("@newsdetails", SqlDbType.NVarChar, 1024).Value = txtnewsdetails.Text;
于 2012-06-18T05:59:42.473 回答
3

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

例如

cmd.Parameters.AddWithValue("@newsdetails","मैं stackoverflow प्यार है");

只需确保表中的newsdetails字段newsactivityNVARCHAR(..)

编辑 这很奇怪。可能 AddWithValue 没有正确推断出参数的类型。您可以明确指定它:

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

明确指定类型和长度的另一个好理由是,如果您在 WHERE 子句过滤器中使用参数。请参阅不带 DBType 的 AddWithValue 导致查询运行缓慢

于 2012-06-18T05:57:37.060 回答
0

SqlDbType.NVarChar在您的参数中使用。

于 2012-06-18T06:30:08.873 回答