Created
May 25, 2014 12:22
-
-
Save niisar/2c4d228506af5dded4ff to your computer and use it in GitHub Desktop.
parameterizing in clause
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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