コード:
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;
}
問題は:
をrowsAffected = 0 111 222 rowsAffected = 1`111, 222
として使用している場合。つまり、値が 1 つしかない場合は更新に成功しますが、複数の値を更新しようとすると失敗します。sqlParam.Value'
, but if I'm using only
or
予期されるクエリ:
UPDATE table
SET active = 0
WHERE id IN ('111', '222');
上記のコードで何が欠けていますか?