我正在N
Unicode
变量名称之前应用string commandText = @"SELECT AccountType,*
FROM Account
WHERE AccountType LIKE N@input ";
使用以下代码我收到以下错误。需要在这里纠正什么?
例外:列名'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;
}
CODE
{{1}}
答案 0 :(得分: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 + "%";
答案 1 :(得分:1)
取自此堆栈Stack overflow
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;
答案 2 :(得分:1)
我看到你正在尝试使用nvarchar参数。我认为.net默认使用.AddWithValue
我不确定你为什么需要对nvarchar进行类型转换,没有“N”部分你应该没问题。
当您要指定字符串文字应被视为nvarchar而不是varchar时,您需要的那部分,如SELECT * from Table where field like N'%VALUE%'
否则,您只需将变量/参数声明为nvarchar
答案 3 :(得分: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;
}