在使用SQLParameter时,如果需要传递一个数组作为参数,可以使用以下方法:
例如:
int[] ids = new int[] {1, 2, 3, 4};
string idString = string.Join(",", ids);
string query = "SELECT * FROM table WHERE id IN (@Ids)";
SqlCommand cmd = new SqlCommand(query, connection);
cmd.Parameters.AddWithValue("@Ids", idString);
例如:
int[] ids = new int[] {1, 2, 3, 4};
string query = "SELECT * FROM table WHERE id IN (" + string.Join(",", ids.Select((s, i) => "@Id" + i)) + ")";
SqlCommand cmd = new SqlCommand(query, connection);
for (int i = 0; i < ids.Length; i++)
{
cmd.Parameters.AddWithValue("@Id" + i, ids[i]);
}
这样就可以将数组作为参数传递给SQL查询语句。