Use of SqlParameter in SQL LIKE clause not working

前端 未结 4 1193
青春惊慌失措
青春惊慌失措 2020-11-27 14:53

I have the following code:

const string Sql = 
    @\"select distinct [name] 
      from tblCustomers 
      left outer join tblCustomerInfo on tblCustomers.         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 15:27

    What you want is:

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

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

    Otherwise, you are either (first sample) searching for the literal "@SEARCH" (not the arg-value), or you are embedding some extra quotes into the query (second sample).

    In some ways, it might be easier to have the TSQL just use LIKE @SEARCH, and handle it at the caller:

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

    Either approach should work.

提交回复
热议问题