0

我正在尝试N在变量名称之前应用如何使用带有 unicode 字符串的“LIKE”语句Unicode

使用以下代码,我收到以下错误。这里需要纠正什么?

例外:列名“N@input”无效。

string commandText = @"SELECT AccountType,* 
                          FROM Account 
                          WHERE AccountType LIKE N@input ";

代码

    static void Main(string[] args)
    {
        string result = DisplayTest("Daily Tax Updates:  -----------------        Transactions");

    }

    private static string DisplayTest(string searchValue)
    {
        string test = String.Empty;
        string connectionString = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            string commandText = @"SELECT AccountType,* 
                          FROM Account 
                          WHERE AccountType LIKE N@input ";
            using (SqlCommand command = new SqlCommand(commandText, connection))
            {
                command.CommandType = System.Data.CommandType.Text;
                command.Parameters.AddWithValue("@input", "%" + searchValue + "%");

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {

                            test = reader.GetString(0);
                        }
                    }
                }
            }
        }

        return test;

    }
4

4 回答 4

4

我看到几个问题。

   string commandText = @"SELECT AccountType,* 
                              FROM Account 
                              WHERE AccountType LIKE N@input";

应该

   string commandText = @"SELECT AccountType,* 
                              FROM Account 
                              WHERE AccountType LIKE @input";

...

 command.Parameters.Add("@input",System.Data.SqlDbType.NVarChar,<<size>>);
 command.Parameters[0].Value = "%" + searchValue + "%";
于 2013-02-12T16:09:50.880 回答
1

取自此堆栈堆栈溢出

SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;
于 2013-02-12T16:03:33.597 回答
1

我看到您正在尝试使用 nvarchar 参数。我认为.net默认情况下会这样做.AddWithValue

我不确定您为什么需要对 nvarchar 进行类型转换,没有“N”部分应该没问题。

当您想要指定字符串文字应被视为 nvarchar 而不是 varchar 时,您需要的那部分,如SELECT * from Table where field like N'%VALUE%'

否则,您只需将变量/参数声明为 nvarchar

于 2013-02-12T16:08:44.713 回答
0

试试这个——

private static string DisplayTest(string searchValue)
{
    string connectionString = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        string commandText = @"SELECT AccountType,* FROM Account WHERE AccountType LIKE @input";

        using (SqlCommand command = new SqlCommand(commandText, connection))
        {
            command.CommandType = System.Data.CommandType.Text;

            command.Parameters.Add("@input", SqlDbType.NVarChar);
            command.Parameters["@input"].Value = string.Format("%{0}%", searchValue);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        return reader.GetString(0);
                    }
                }
            }
        }
    }

    return String.Empty;
}
于 2013-04-24T05:24:37.060 回答