简体   繁体   English

Oracle / C#:如何将绑定变量与select语句一起使用以返回多个记录?

[英]Oracle/c#: How do i use bind variables with select statements to return multiple records?

I have a question regarding Oracle bind variables and select statements. 我对Oracle绑定变量和选择语句有疑问。

What I would like to achieve is do a select on a number different values for the primary key. 我想实现的是对多个不同的主键值进行选择。 I would like to pass these values via an array using bind values. 我想通过使用绑定值的数组传递这些值。

select * from tb_customers where cust_id = :1

int[] cust_id = { 11, 23, 31, 44 , 51 };

I then bind a DataReader to get the values into a table. 然后,我绑定一个DataReader以将值放入表中。

The problem is that the resulting table only contains a single record (for cust_id=51 ). 问题在于结果表仅包含一个记录(对于cust_id=51 )。 Thus it seems that each statement is executed independently (as it should), but I would like the results to be available as a collective (single table). 因此,似乎每个语句都是独立执行的(应该如此),但是我希望结果可以作为一个集合(单个表)使用。

A workaround is to create a temporary table, insert all the values of cust_id and then do a join against tb_customers . 一种解决方法是创建一个临时表,插入cust_id所有值,然后对tb_customers进行tb_customers The problem with this approach is that I would require temporary tables for every different type of primary key, as I would like to use this against a number of tables (some even have combined primary keys). 这种方法的问题在于,我需要为每种不同类型的主键都使用临时表,因为我想对许多表(有些甚至组合主键)使用临时表。

Is there anything I am missing? 我有什么想念的吗?

Not asking the question as to why you would want to do this to begin with. 不问关于为什么要这样做的问题。 Shouldn't the sql statement be something like sql语句不应该像

select * from tb_customers where cust_id = 11 or 23 or ...

Edit: 编辑:

I am limited in Oracle but when I look at the documentation I think that you might have to do something like this: 我受Oracle的限制,但是当我查看文档时,我认为您可能必须执行以下操作:

variable i number
exec :i := 11
select * from tb_customers where cust_id = :i

This would allow you to take advantage of binding. 这将使您能够利用绑定。 You will have to add each record return to your own collection since it will still only return one at a time. 您将必须将每个记录返回添加到您自己的集合中,因为它一次仍只会返回一个。

I know this was asked a while ago but not a brilliant answer. 我知道这是前一段时间提出的,但不是一个很好的答案。

I would do something like this - please excuse the crude psudo code 我会做这样的事情-请原谅伪装的伪代码

string bindList = "";
for(int ii=0;ii<cust_id.count;++ii)
{
  if(ii == 0)
  {
   bindList += ":" + ii;
  }
  else
  {
   bindList += ",:" + ii;
  }
  OracleParameter param = new OracleParameter();
  param.dbType = types.int;
  param.value = cust_id[ii];
  command.Parameters.Add(param);
}

query = "select * from tb_customers where cust_id in(" + bindList + ")";

So then query ends up having in(:1,:2,:3,etc) and each of these are bound separately. 因此,查询最终将具有in(:1,:2,:3,etc),并且每个都分别绑定。

There is also a similar question here: OracleParameter and IN Clause 这里还有一个类似的问题: OracleParameter和IN子句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 oracle如何在c#中使用多个select查询 - How to use multiple select queries inside c# by oracle 如果需要在INSERT调用之间进行SELECT调用,如何在C#中将准备好的语句与SQLite一起使用? - How do I use prepared statements with SQLite in C# if I need to do a SELECT call between INSERT calls? C#,如何在if语句上使用dateTimePicker的值? - C#, how do I use a dateTimePicker's value on if statements? C#如何返回多行参数数据 - How do I return multiple rows of parameter data with C# 如何从Asp.Net/C# Codebehind中的单个SQL Select语句返回多个字符串变量? - How to return multiple string variables from a single SQL Select Statement in Asp.Net/C# Codebehind? 如何一次选择多个控件(按钮)C# - How do I select multiple controls(buttons) at a time c# 如何使用参数并在C#中返回? - How do I use parameters and return in C#? 如何在C#IF语句中使用此示例“ return”? - How do I make use of this examples “return”, in a C# IF statement? 如何获取使用顶级语句的 C# 9 程序的反射类型信息? - How do I get the Reflection TypeInfo of a C# 9 program that use Top-level statements? 如何在mysql和c#中的列表中选择子表的多个记录? - How to get select multiple records of subtable in lists in mysql and c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM