如何通过参数设置为SQL'在'语句? [英] How to pass parameter to sql 'in' statement?

查看:121
本文介绍了如何通过参数设置为SQL'在'语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建这个查询:

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

但我不能找到Npgsql和NpgsqlParameter achiving本的任何例子。我想是这样的:

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

推荐答案

把它作为一个数组:

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

这篇关于如何通过参数设置为SQL'在'语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆