在 SQL LIKE 子句中使用 SqlParameter 不起作用

问题描述 投票:0回答:6

我有以下代码:

const string Sql = 
    @"select distinct [name] 
      from tblCustomers 
      left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId  
      where (tblCustomer.Name LIKE '%@SEARCH%' OR tblCustomerInfo.Info LIKE '%@SEARCH%');";

using (var command = new SqlCommand(Sql, Connection))
{       
    command.Parameters.AddWithValue("@SEARCH", searchString);
    ...
}

这不起作用,我也尝试过:

const string Sql = 
    @"select distinct [name] 
     from tblCustomers 
     left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId  
     where (tblCustomer.Name LIKE @SEARCH OR tblCustomerInfo.Info LIKE @SEARCH );";

using (var command = new SqlCommand(Sql, Connection))
{       
    command.Parameters.AddWithValue("@SEARCH", "'%" + searchString + "%'");
    ...
}

但这也不起作用。出了什么问题?有什么建议吗?

c# sql-server t-sql ado.net sql-like
6个回答
204
投票

你想要的是:

tblCustomerInfo.Info LIKE '%' + @SEARCH + '%'

(或编辑参数值以将 % 放在第一位)。

否则,您要么(第一个示例)搜索literal“@SEARCH”(不是参数值),要么在查询中嵌入一些额外的引号(第二个示例)。

在某些方面,让 TSQL 仅使用

LIKE @SEARCH
并在调用者处处理它可能会更容易:

command.Parameters.AddWithValue("@SEARCH","%" + searchString + "%");

任何一种方法都应该有效。


10
投票

而不是使用:

const string Sql = 
@"select distinct [name] 
  from tblCustomers 
  left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId  
  where (tblCustomer.Name LIKE '%@SEARCH%' OR tblCustomerInfo.Info LIKE '%@SEARCH%');";

使用此代码:

const string Sql = 
@"select distinct [name] 
  from tblCustomers 
  left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId  
  where (tblCustomer.Name LIKE '%' + @SEARCH + '%' OR tblCustomerInfo.Info LIKE '%' + @SEARCH + '%');";

7
投票

请注意 AddAddWithValue 方法之间的细微差别。当我使用 Add 方法并输入错误的 SqlType 参数时,我遇到了以下问题。

  • nchar
    nvarchar
    可以存储Unicode字符。
  • char
    varchar
    无法存储 Unicode 字符。

例如:

string query = " ... WHERE stLogin LIKE @LOGIN ";

SqlParameter p = new SqlParameter("@LOGIN", SqlDbType.Char, 255) 
{ 
    Value = "%" + login + "%" 
};

command.Parameters.AddWithValue(p.ParameterName, p.Value); //works fine!!!

command.Parameters.Add(p); // won't work

当我将 SqlType 更改为 NVarChar 时,这两种方法对我来说效果很好。

SqlParameter p = new SqlParameter("@LOGIN", SqlDbType.NVarChar, 255) 
{ 
    Value = "%" + login + "%" 
};

command.Parameters.AddWithValue(p.ParameterName, p.Value); //worked fine!!!

command.Parameters.Add(p); //worked fine!!!

0
投票

小心使用固定长度的 SqlDbType 参数:SqlDbType.NChar 和 SqlDbType.Char。

string arcid = "ACK";

// does not work, length arcid + '%' != 10
cmd.Parameters.Add("@arcid", SqlDbType.Char, 10).Value = arcid + '%';

// works fine, length arcid + '%' == 4
cmd.Parameters.Add("@arcid", SqlDbType.Char, 4).Value = arcid + '%';

-1
投票

其实你可以直接添加值而不是使用参数。例如,这就是我实现它的方式,并且运行良好。干杯!

string name = "test";
string  nameCondition= " and FirstName like '%"+ name +"%' or MiddleName like '%" + name +"%' or LastName like '%"+name+"%'";


-7
投票

您可以执行

LIKE @SEARCH
并在 C# 代码中执行

searchString = "%" + searchString + "%"
© www.soinside.com 2019 - 2024. All rights reserved.