0

ユニコード文字列で「LIKE」ステートメントを使用する方法で説明したように、N変数名の前に適用しようとしていますか?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

nvarcharパラメータを使用しようとしているようです。.netはデフォルトでそれを行うと思います.AddWithValue

なぜnvarcharへの型キャストが必要なのかわかりません。「N」の部分がなくても問題ないはずです。

文字列リテラルをvarcharではなくnvarcharとして扱うように指定する場合に必要な部分は、次のようになります。SELECT * from Table where field like N'%VALUE%'

それ以外の場合は、変数/パラメーターをnvarcharとして宣言するだけです。

于 2013-02-12T16:08:44.713 に答える
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 に答える
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 に答える