如何将参数传递给sql“in”陈述?

发布于 2024-11-02 04:49:02 字数 515 浏览 1 评论 0原文

我想创建这个查询:

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

但没有成功;)

I want to create this query:

select * from products where number in ('123', '234', '456');

but I can't find any example of achiving this with Npgsql and NpgsqlParameter. I tried like this:

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

but it didn't work ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黑寡妇 2024-11-09 04:49:02

将其作为数组传递:

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

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);
心舞飞扬 2024-11-09 04:49:02

您需要动态创建命令字符串 - 循环,第一个参数为 :num0,第二个参数为 :num1 等。添加完所有参数后,删除最后一个字符“,”并将其替换为“)”。

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

后来的我们 2024-11-09 04:49:02

除了@Quassnoi 的答案之外,我还将添加这个答案来展示我们如何在实际代码中完成它。

警告!该工作代码来自真实项目,可能会损坏您的漂亮方法!

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

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

Warning! This working code is from real project and can damage your beautiful 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);
明媚殇 2024-11-09 04:49:02

使用“从 id IN 的测试中删除(选择 unnest(@ids))”

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

挽容 2024-11-09 04:49:02

更新了〜2021年的答案:

您可以像这样使用ANY

WHERE id = ANY (@ids)

然后,当您添加参数时,请执行以下操作:

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

Updated answer for ~2021:

You can use ANY like this:

WHERE id = ANY (@ids)

Then when you add your params, do this:

cmd.Parameters.AddWithValue("ids", ids.ToArray());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文