Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get contain letter in c# like in sql

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 ‘%’.’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

query in c#:

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

>Solution :

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, '%')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading