Home > database >  How to get contain letter in c# like in sql
How to get contain letter in c# like in sql

Time:05-26

In sql, i use this query to get contain letter:

select users_id, users_name, users_phone, users_address, users_email from dbo.tblUser
             where users_name like 'usersname%'

But when i use the same query in c#, i get this:

System.Data.SqlClient.SqlException: 'Incorrect syntax near '%'.'

query in c#:

select users_id, users_name, users_phone, users_address, users_email from dbo.tblUser
             where users_name like @usersname%

CodePudding user response:

The reason for the error is that you are adding an unknown symbol in your query. There are no quotes around it and also no concatenation is used.

One way to fix that is to add the % character to the parameter's value in the c# code. Example:

param.Value = userNameVariable   "%";
WHERE users_name LIKE @usersname

Another option is to concatenate it in the query.

WHERE users_name LIKE @usersname   '%'

or

WHERE users_name LIKE CONCAT(@usersname, '%')
  • Related