Performing an IN Query with a List of Strings using SQL Parameters
Your code attempts to execute an IN statement using a List
To execute an IN query with a list of strings, you can follow these steps:
string sql = "SELECT dscr FROM system_settings WHERE setting IN ({0})";
string[] paramArray = settingList.Select((x, i) => "@settings" + i).ToArray();
cmd.CommandText = string.Format(sql, string.Join(",", paramArray));
for (int i = 0; i < settingList.Count; ++i) { cmd.Parameters.Add(new SqlParameter("@settings" + i, settingList[i])); }
By following these steps, you can safely perform an IN query with a list of strings using SqlCommand parameters. This approach involves creating a custom parameter name for each string in the list and specifying the appropriate data type for the SqlParameter.
The above is the detailed content of How to Use SQL Parameters with an IN Clause and a List of Strings?. For more information, please follow other related articles on the PHP Chinese website!