C # MySQL as a query does not accept parameters

I use a query to search for keywords in a specific field, when I put @parameter and then add a parameter with a value that it doesn’t work, however, when I enter the value directly, it works, anyone can help me pass the value as a parameter to my request , you are welcome? Below are my codes:

This works and retrieves any entry with the word "My" in its name.

string cmdText = "SELECT  * FROM tblshareknowledge where title LIKE '%My%'";
cmd = new MySqlCommand(cmdText, con);
//cmd.Parameters.AddWithValue("@myTitle", title);

This does not work:

string cmdText = "SELECT  * FROM tblshareknowledge where title LIKE '@myTitle'";
cmd = new MySqlCommand(cmdText, con);
cmd.Parameters.AddWithValue("@myTitle", title); 
+5
source share
1 answer

You are currently entering your parameter in quotation marks, which means that it is no longer used as a parameter. I suspect you want:

string cmdText = "SELECT * FROM tblshareknowledge where title LIKE @myTitle";
cmd = new MySqlCommand(cmdText, con);
cmd.Parameters.AddWithValue("@myTitle", "%" + title + "%");
+13
source

All Articles