C# 4.0 NpqsqlParameter-多个值
代码:C# 4.0 NpqsqlParameter-多个值,c#-4.0,npgsql,C# 4.0,Npgsql,代码: string sqlCommand = @"UPDATE table SET active = 0 WHERE id IN (@CommaSeparatedId)"; string sqlParamName = "CommaSeparatedId"; string sqlParamValue = "111, 222"; try { using (NpgsqlConnection connection = new NpgsqlConnection()) {
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;
}
UPDATE table
SET active = 0
WHERE id IN ('111', '222');
问题是:
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;
}
UPDATE table
SET active = 0
WHERE id IN ('111', '222');
如果我使用111、222
作为sqlParam.Value'
rowsAffected=0,但如果我只使用111或222
rowsAffected=1`。这意味着仅更新1个值会成功,但如果尝试更新多个值则会失败
预期查询:
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;
}
UPDATE table
SET active = 0
WHERE id IN ('111', '222');
我在上面的代码中缺少了什么?您所面临的问题是,数据库引擎将看到参数值“111222”,而不是两个不同的值,而是一个。
数据库将搜索ID为“111222”的记录,但未找到与请求匹配的内容。
您应该尝试使用存储过程,并根据PostgreSQL所需的语法执行正确的。从您的代码中,发送sql(如果启用它,可以在Postgresql日志中看到)如下所示:updatetablesetactive=0,其中id位于('111222');我们有一个功能请求,正是为了满足您一次添加多个值的需要,但我们没有实现它,而且我们认为它不会实现,因为这种逗号分隔的处理应该由上层的某个东西来完成,可能由ORM来完成。只是在我们的跟踪器中找到了该功能请求。在那里你可能会发现一些关于如何解决这个问题的有用信息:我希望它能有所帮助。