In the following example, I create a query to make a large insert into a MySQL database:
const string QUERY = "INSERT INTO contacts (first_name,last_name) VALUES{0};"; public string BuildQuery(IEnumerable<contact> contacts) { List<string> values = new List<string>(); foreach (var contact in contacts) { values.Add(string.Format("('{0}','{1}')", contact.first_name, contact.last_name)); } return string.Format(QUERY, string.Join(",", values)); }
The result might look something like this:
INSERT INTO contacts (first_name,last_name) VALUES("J","Kappers"),("A","Temple")
What can I do to write a more secure query that is not subject to SQL Injection?
source share