Unicode的SQL查询W /参数替代N前缀 [英] Unicode SQL Query W/ Parameter instead N Prefix

查看:189
本文介绍了Unicode的SQL查询W /参数替代N前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个插入查询,从C#对SQL Server数据库中执行。

I have an insert query to execute from within a C# against a SQL Server database.

我插入到该列类型为nvarchar。

The column I am inserting to is of type nvarchar.

我插入到该列中的数据是非英语。

the data I am inserting to that column is non-english.

是不是足以让我为了使用AddWithValue来非英语数据传递到服务器?像这样的例子:

Is it sufficient for me to use AddWithValue in order to pass the non-english data to the server? like this example:

string dogName = "עברית";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    using (SqlCommand command = new SqlCommand("INSERT INTO Dogs1(Name) VALUES @Name", connection))
    {
    command.Parameters.AddWithValue("Name", dogName);
    command.ExecuteNonQuery();
    }
}



或者我必须使用N个前缀声明它的unicode ?喜欢它是这么说的 href=\"http://support.microsoft.com/kb/239530\">。

Or must I use the N prefix to declare it unicode? like it says so here.

推荐答案

如果我理解正确的问题,你可以明确设置的SqlCommand参数是一个特定的数据类型。您将可以将其设置为nvarchar的如由下面的链接: HTTP: //msdn.microsoft.com/en-us/library/yy6y35y8.aspx

If I am understanding the question correctly, you can explicitly set the SqlCommand parameter to be a specific data type. You will be able to set it to be nvarchar as shown by the following link: http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx

这下面的代码片段是直接从MSDN采取:

This below code snippet is taken directly from MSDN:

SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;

这使用了一个明确创建的SqlParameter实例,但是它是通过索引的SqlCommand的SqlParameterCollection同样的想法实例。

This uses an explicitly created SqlParameter instance, but it is the same idea by indexing the SqlParameterCollection of the SqlCommand instance.

这篇关于Unicode的SQL查询W /参数替代N前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆