在 C# 中添加 ID 列表作为 Firebird 查询的参数

Add list of IDs as parameter for Firebird query in C#

我想更新由 ID 列表给出的行列表。通常,查询的参数可以通过参数属性来传递。但是如果我传递一个 ID 列表,Firebird 将不会接受查询。我正在使用来自 NuGet 的 FirebirdSql.Data.FirebirdClient。

代码(简体):

List<string> ids = someList.Select(_ => _.Id).ToList();
using (var fbCommand = new FbCommand("UPDATE someTable SET NAME='foo' WHERE ID IN (@ids)", fbConnection))
{
    fbCommand.Parameters.Add("ids", ids);
    fbCommand.ExecuteNonQuery();
}

Table "someTable" 是这样定义的:

CREATE TABLE someTable (
    ID CHAR(36),
    NAME CHAR(20)
);

这是例外情况:

Exception thrown: 'FirebirdSql.Data.FirebirdClient.FbException' in FirebirdSql.Data.FirebirdClient.dll An exception of type 'FirebirdSql.Data.FirebirdClient.FbException' occurred in >FirebirdSql.Data.FirebirdClient.dll but was not handled in user code arithmetic exception, numeric overflow, or string truncation string right truncation

通过简单地使用 foreach 循环更新所有行,我解决了这个问题:

List<string> ids = someList.Select(_ => _.Id).ToList();
foreach (string id in ids) {
    using (var fbCommand = new FbCommand("UPDATE someTable SET NAME='foo' WHERE ID = @id", fbConnection))
    {
        fbCommand.Parameters.Add("id", id);
        fbCommand.ExecuteNonQuery();
    }
}