有一个组合键列表,一个人如何编码一个ODP.NET命令以选择所有带有组合键的行?
select * from table t
where (t.A, t.B, t.C) in (
('key1A','key1B', 1),
('key2A','key2B', 2),
('key3A','key3B', 3)
)
我有以下示例代码:
class TableKey
{
public string ColumnA {get;set;}
public string ColumnB {get;set;}
public long ColumnC {get;set;}
}
List<TableKey> listOfKeys = GetListOfKeys();
// should translate to:
// select * from table t
// where (t.A, t.B, t.C) in (
// ('key1A','key1B', 1),
// ('key2A','key2B', 2),
// ('key3A','key3B', 3)
// )
var query = "select * from table t where (t.A, t.B, t.C) in (:keys)";
using (var con = new OracleConnection(_connectionString))
using (OracleCommand cmd = con.CreateCommand())
{
cmd.CommandText = query;
cmd.BindByName = true;
cmd.ArrayBindCount = listOfKeys.Count;
// How should the parameters be populated?
// cmd.Parameters.Add("keys", OracleDbType.???).Value = listOfKeys.ToArray();
await con.OpenAsync().ConfigureAwait(false);
using (DbDataReader reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
{
var result = new List<TableObject>();
while (await reader.ReadAsync().ConfigureAwait(false))
{
// create object from reader and add it to the list
}
return result;
}
}
我看过这样的question,它说明了我想在odp.net中做的事情
我也尝试了以下方法,但是它总是返回并出现文件结尾的oracle异常:
var query = "select * from table t where t.A = :keyA and t.B = :keyB and t.C = :keyC"
for(var i = 0; i < listOfKeys.Count; i++)
{
arrayKeyA[i] = listOfKeys[i].ColumnA;
arrayKeyB[i] = listOfKeys[i].ColumnB;
arrayKeyC[i] = listOfKeys[i].ColumnC;
}
cmd.Parameters.Add("keyA", OracleDbType.Varchar2).Value = arrayKeyA;
cmd.Parameters.Add("keyB", OracleDbType.Varchar2).Value = arrayKeyB;
cmd.Parameters.Add("keyC", OracleDbType.Decimal).Value = arrayKeyC;