1

I would like to update a list of rows given by a list of IDs. Normally, the parameters of the query can be passed via the parameter property. But if I pass a list of IDs, Firebird will not accept the query. I'm using the FirebirdSql.Data.FirebirdClient from NuGet.

Code (simplified):

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();
}

The Table "someTable" is defined like this:

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

This is the exception:

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

16
  • Fix : fbCommand.Parameters.Add("@ids", ids); Commented Nov 13, 2020 at 15:13
  • I still get the same exception using "@ids" as parameter name... Commented Nov 13, 2020 at 15:22
  • The ID in the database is has max size of 36 characters. Is the ID in c# exceeding 36 characters? Commented Nov 13, 2020 at 15:26
  • there are some tricks, but in general a parameter is not scalar, not value. Is your ID only integer and nothoing else ? Commented Nov 13, 2020 at 15:36
  • 1
    @Miamy True, and you can make it parametrized like where ID IN (@Param1, @Param2, @Param3, @Param4). You can have many params, indeed. But each param will be one scalar value. Equally in C+ you can have int i1=1, i2=2, i3=3, i4=4; but it will be not one variable holding four numbers, it would be four variables holding one number each. Same with params. There is no text-to-text translation, there is not "string splicing" or "stirng interpolation" or what is the name. There really is a creation of variables and putting values into variables OUTSIDE of SQL query text. And that is intention! Commented Nov 13, 2020 at 15:47

1 Answer 1

0

I got kind of around the problem by simply using a foreach loop to update all the rows:

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Bad structure: the query is loop invariant. using should be outside the for not inside. Creating the query, adding the parameter and PREPARING the query ("compiling" it in SQL lingo) should be done before the loop. Then inside the loop you have to change parameter value and execute the previously prepared query.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.