Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIKE doesn't seem to work

Tags:

c#

mysql

I´working on a C# desktop App and want to search in a MYSQL database all the records that match with a specific name in order to display this records into a datagridview but got the error when executing the query with the value: pablo

enter image description here

this is my query

sentencia = "select * from registro where nombreParticipante LIKE '%' + @valor + '%'";
                    nombre = valor.ToUpper();
                    cmd.Parameters.AddWithValue("@valor", nombre);
                    cmd.CommandText = sentencia;

could you tell me what is wrong with the code please

like image 300
Pablo Tobar Avatar asked Mar 13 '26 00:03

Pablo Tobar


2 Answers

Try this code :

"select * from registro where nombreParticipante LIKE '% + @valor + %'";

The correct syntax of LIKE is :

WHERE 'STRING' LIKE '%OtherString%' 

And what you did is:

WHERE 'STRING' LIKE '%'OtherString'%' 
like image 193
sagi Avatar answered Mar 15 '26 13:03

sagi


Format the string for your parameter rather than in the query:

sentencia = "select * from registro where nombreParticipante LIKE @valor";
                    nombre = valor.ToUpper();
                    cmd.Parameters.AddWithValue("@valor", "%" + nombre + "%");
                    cmd.CommandText = sentencia;
like image 30
Owen Pauling Avatar answered Mar 15 '26 12:03

Owen Pauling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!