代码:
string sqlCommand = @"UPDATE table SET active = 0 WHERE id IN (@CommaSeparatedId)";
string sqlParamName = "CommaSeparatedId";
string sqlParamValue = "111, 222";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
// Get connection string from Web.config
connection.ConnectionString = _connectionString;
connection.Open();
Int32 rowsAffected;
using (NpgsqlCommand command = new NpgsqlCommand(sqlCommand, connection))
{
NpgsqlParameter sqlParam = new NpgsqlParameter(sqlParamName, NpgsqlTypes.NpgsqlDbType.Varchar);
// Code below no exception occur, and active not updated to 0
// sqlParam.Value = sqlParamValue;
// This code works for only one value
sqlParam.Value = "111";
command.Parameters.Add(sqlParam);
rowsAffected = command.ExecuteNonQuery();
}
}
}
catch (NpgsqlException pgEx)
{
throw pgEx;
}
问题是:
如果我使用111, 222
rowsAffected sqlParam.Value'
= 0 , but if I'm using only
111 or
222 rowsAffected = 1`。这意味着当只有 1 个值时更新成功,但如果尝试更新超过 1 个值则失败。
预期查询:
UPDATE table
SET active = 0
WHERE id IN ('111', '222');
我在上面的代码中缺少什么?