<分区>
下面的查询有一个要分配字符串列表的参数:
select * from a_table where something in :list_of_strings
我有一个 C# List<string>
我想分配给 list_of_strings
参数。
给定一个 OracleCommand
(代表上面的查询),我怎样才能绑定(bind)我的 List<string>
命令的 list_of_strings
参数?
<分区>
下面的查询有一个要分配字符串列表的参数:
select * from a_table where something in :list_of_strings
我有一个 C# List<string>
我想分配给 list_of_strings
参数。
给定一个 OracleCommand
(代表上面的查询),我怎样才能绑定(bind)我的 List<string>
命令的 list_of_strings
参数?
最佳答案
实际上,您不能将单个参数绑定(bind)到值列表。在这种情况下,您可以将值连接到查询字符串。
但是,不建议这样做,因为您可以在 IN
子句中放置的值是有限的。
List<string> list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("3");
list.Add("4");
string listStr = string.Join(",", list);
//result: "1,2,3,4"
如果您的字符串列表是一个字符串列表,您可以这样做:
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
list.Add("three");
list.Add("four");
string listStr = string.Concat("'", string.Join("','", list), "'");
//result: "'one','two','three','four'"
查询字符串:
string query = string.Format("select * from a_table where something in({0})", listStr);
观察:您可能必须处理列表为空的可能性。
另一种可能性是将所有值插入临时表并在 select 语句中使用它。与连接技术相比,这将具有无限字符串值和避免在 DBMS 上进行新的硬解析的优势:
SELECT *
FROM A_TABLE
WHERE SOMETHING IN(SELECT SOMETHING FROM TEMP_TABLE)
关于c# - 在 C# 中将字符串列表添加到 OracleCommand.Parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14798839/