@Charles: you are going in the right direction, but we use parameterized queries mainly to prevent SQL injection. Entering "external" values ββ( params string[] args ) hardcoded in the queries causes problems. You can repeat the arguments, but you still have to use the following parameters:
string[] values = new [] {"value1", "value2", "value3", "value4"}; StringBuilder query = new StringBuilder("Select * From Table Where Column in ("); SqlCommand cmd = new SqlCommand(); cmd.Connection = new SqlConnection("Your connection string"); for(int i = 0; i < columns.Length; i++) { string arg = string.Format("@arg{0}", i); cmd.Parameters.AddwithValue(arg, SanatizeSqlString(columns[i])); sb.AppendFormat("{0}, ", arg); } sb = sb.Remove(sb.Length -2, 2); sb.Append(")"); cmd.CommandText = sb.ToString();
Thus, you will receive a request such as:
select * from table where column in (@arg0, @arg1, @arg2, @arg3)
source share