[ACCEPTED]-How to pass parameter to sql 'in' statement?-npgsql

Accepted answer
Score: 39

Pass it as an array:

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

0

Score: 1

You need to dynamically create your command 4 string - loop with your first parameter 3 as :num0, second as :num1 etc. When you've 2 added all of them, remove the last character 1 "," and replace it with ")".

Score: 1

In addition to @Quassnoi answer I'll add 4 this one to show, how we done it in real 3 code.

Warning! This working code is from 2 real project and can damage your beautiful 1 approaches!

string commstr = "SELECT product_name, price, product_url, image_url FROM products WHERE id  = ANY(@arr);";
NpgsqlCommand cm = new NpgsqlCommand(commstr, cn);
NpgsqlParameter arpar = new NpgsqlParameter();
arpar.ParameterName = "arr";
arpar.NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Bigint;
arpar.Value = PerformQuerySphinx(query, limit);
cm.Parameters.Add(arpar);
Score: 0

use "delete from test where id IN (select 1 unnest(@ids))"

Score: 0

Updated answer for ~2021:

You can use ANY like this:

WHERE id = ANY (@ids)

Then when you add 1 your params, do this:

cmd.Parameters.AddWithValue("ids", ids.ToArray());

More Related questions