There is nothing elegant to solve this problem. You can use the IN clause, but you still need to build the parameters one by one. Therefore, instead of returning a string, you can return OdbcCommand prepared with all the parameters required by your list of names. You just need to add the connection and execute (or transfer the connection as well and prepare everything here)
public OdbcCommand MakeCommand(List<string> names, OdbcConnection con) { List<OdbcParameter> parameters = new List<OdbcParameter>(); List<string> placeholders = new List<string>(); foreach(string name in names) { OdbcParameter p = new OdbcParameter("?", OdbcType.NVarChar); p.Value = name; parameters.Add(p); placeholders.Add("?") } string command = "SELECT name, value FROM my_table WHERE name IN("; command = command + string.Join(",", placeholders.ToArray()) + ")"; OdbcCommand cmd = new OdbcCommand(); cmd.CommandText = command; cmd.Connection = con; cmd.Parameters.AddRange(parameters.ToArray()); return cmd; }
If you still have problems, this may be due to the DataType options. NVarChar, VarChar, Text, String. Some db may respond differently to this type. In which database are you testing this code?
Steve source share