[ACCEPTED]-How to pass parameter to sql 'in' statement?-npgsql
Accepted answer
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
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 ")".
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);
use "delete from test where id IN (select 1 unnest(@ids))"
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());
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.