Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# MySQL like query not taking parameters

Tags:

c#

mysql

I am using a query to find keywords in a particular field, when i put the @parameter and then addparameter with value it does not work, however when i input the value directly, it works, anyone can help me pass value as parameter to my query please? below are my codes:

This works and retrieves any record with the word "My" in its title.

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

This one does not work:

string cmdText = "SELECT  * FROM tblshareknowledge where title LIKE '@myTitle'";
cmd = new MySqlCommand(cmdText, con);
cmd.Parameters.AddWithValue("@myTitle", title); 
like image 996
avi Avatar asked Feb 17 '13 08:02

avi


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


1 Answers

You're currently putting your parameter within quotes, which means it's no longer being 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 + "%");
like image 126
Jon Skeet Avatar answered Sep 24 '22 00:09

Jon Skeet