I am trying to build a MySQL query in my C # application, I am wondering if the OR statement can be used in MySQL parameters.
I have a list of names, and I want to check and see what names already exist in the database. Here is a short example
List<string> names = new List<string> {"adam", "bob", "cathy"};
MySqlConnection connection = getAndOpenConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM Employees WHERE name = @names";
command.Parameters.Add(new MySqlParameters("@names", String.Format("names = {0}", String.Join(names, " or name = "))));
My initial idea is to build the command text as follows:
command.CommandText = String.Format("SELECT * FROM Employees WHERE name = '{0}'", String.Join(names, "' or name = '"))
The code will give me the correct command text that I want, but I really want to prevent SQL injection.
Can someone please help me on how to build MySqlCommand correctly?
source
share