Skip to content

Instantly share code, notes, and snippets.

@niisar
Created May 25, 2014 12:22
Show Gist options
  • Save niisar/2c4d228506af5dded4ff to your computer and use it in GitHub Desktop.
Save niisar/2c4d228506af5dded4ff to your computer and use it in GitHub Desktop.
parameterizing in clause
string[] tags = new string[] { "ruby", "rails", "scruffy", "rubyonrails" };
string cmdText = "SELECT * FROM Tags WHERE Name IN ({0})";
string[] paramNames = tags.Select(
(s, i) => "@tag" + i.ToString()
).ToArray();
string inClause = string.Join(",", paramNames);
using (SqlCommand cmd = new SqlCommand(string.Format(cmdText, inClause))) {
for(int i = 0; i < paramNames.Length; i++) {
cmd.Parameters.AddWithValue(paramNames[i], tags[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment