Select DB2 中使用数组的计数查询

Select Count query in DB2 with Array

我正在尝试使用一个查询来计算我的 DB2 数据库中的所有记录,其中使用数组的内容来搜索 Table。

           string[] strNumbers = txtNumbers.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

               string strSearch = "";
                   strSearch = "SELECT COUNT(*) FROM TABLENAME WHERE NUMBER = '" + strNumbers + "' AND COMMENT = '" + strMessage + "'";

                DB2Command cmdSearchTable = new DB2Command(strSearch, db2Connection);

                int nodeCount = 0;
                nodeCount = int.Parse(cmdSearchTable.ExecuteScalar().ToString());

if (nodeCount == 0) {
 Not Found          
 } else
  { Found
   }

此代码抛出异常 ({"The value of a host variable in the EXECUTE or OPEN statement is out of range for its corresponding use."})

但是,如果我使用 strNumbers.Length,它不会抛出错误,但 nodeCount 仍然为 0。

我需要循环还是其他? 查询和数据库连接很好,因为我能够 Select,从同一个程序插入数据库。

谢谢

编辑 - 我已经设法解决了这个问题(参见已接受的答案)但现在又遇到了另一个问题。假设“1234567”在数据库中,一旦它 return 1 就可以了。如果输入不在数据库中的 '5551234' returns 0 也可以。但是问题在于,如果输入是“1234567,5551234”,计数将 return 1,因为 1234567 在数据库中,即使 5551234 不是。

有没有办法让 1234567 输出 1,然后 5551234 输出 0?

尝试更改

WHERE NUMBER = '" + strNumbers + "' AND

WHERE NUMBER IN (" + strNumbers + ") AND

确保 strNumbers 以逗号分隔。像这样的 '12345'、12346'、'12347' 或 12345,12346,12347 取决于字段 NUMBERS 的数据类型。