C# · 12月 30, 2021

c# – 如何将参数传递给sql’in’语句?

我想创建这个查询: select * from products where number in (‘123′,’234′,’456’);

但我找不到任何与Npgsql和NpgsqlParameter一起使用的示例.我试过这样:

string[] numbers = new string[] { “123”,”234″ };NpgsqlCommands cmd = new NpgsqlCommands(“select * from products where number in (:numbers)”);NpgsqlParameter p = new NpgsqlParameter(“numbers”,numbers);command.Parameters.Add(p);

但它没有工作;)

解决方法 将其作为数组传递: string[] numbers = new string[] { “123”,”234″ };NpgsqlCommands cmd = new NpgsqlCommands(“select * from products where number = ANY(:numbers)”);NpgsqlParameter p = new NpgsqlParameter(“numbers”,NpgsqlDbType.Array | NpgsqlDbType.Text);p.value = numbers;command.Parameters.Add(p);