Using SqlParameter in SQL LIKE not working

I have the following code:

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); ... } 

This does not work, I tried this too:

 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 + "%'"); ... } 

but it does not work. What is going wrong? Any suggestions?

+54
c # sql-server tsql sql-like
Mar 20 '09 at 5:58
source share
4 answers

What would you like:

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

(or edit the parameter value to include% first).

Otherwise, you either (the first sample), look for the letter "@SEARCH" (not the arg value), or insert some additional quotation marks in the query (second sample).

In a sense, it might be easier to have TSQL just use LIKE @SEARCH and process it at the caller:

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

Any approach should work.

+100
Mar 20 '09 at 6:02
source share

Instead of using:

 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%');"; 

Use this code:

 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 + '%');"; 
+2
Sep 10 '16 at 7:42 on
source share

Just a little more careful with the slight difference between the Add and AddWithValue methods . I had a problem below when I used the Add method and set the wrong SqlType parameter.

  • nchar and nvarchar can store Unicode characters .
  • char and varchar cannot store Unicode characters .

For example:

 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 

When I changed SqlType to NVarChar , both methods worked fine for me.

 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
Jan 31 '19 at 13:00
source share

You can do LIKE @SEARCH and in your C # code do

 searchString = "%" + searchString + "%" 
-5
Mar 20 '09 at 6:18
source share



All Articles