我正在寻找以下Java技术的C#ODAC改编,其中代码能够将数字数组(数组大小可以变化)绑定到非PL / SQL SELECT
语句中,然后在WHERE ID IN (...)
样式检查中使用结果数组。
http://rafudb.blogspot.com/2011/10/variable-inlist.html
这是在基于OCI的C程序中使用的这种技术的另一个例子:
Oracle OCI, bind variables, and queries like ID IN (1, 2, 3)
具体来说,我们想要执行像
这样的查询SELECT * FROM MyTable WHERE ID IN (SELECT * FROM TABLE(:1))
将一系列Numbers传入:1
。
答案 0 :(得分:3)
oraParam.UdtTypeName = "SYS.ODCINUMBERLIST";
VArray newArray = new VArray();
newArray.Array = new Int32[] {12,24,42};
oraParam.OracleDbType = OracleDbType.Array;
oraParam.Value = newArray;
string query = @"Select * from TABLE(:1) ";
OracleCommand command = new OracleCommand(query, MyConnection);
command.Parameters.Add(oraParam);
OracleDataReader reader;
var m_connection = new OracleConnection("The CONNECTION STRING");
m_connection.Open();
var reader = command.ExecuteReader();
reader.Close();
m_connection.Close();
接下来是以下帮助程序类:
class VArray : IOracleCustomType, INullable
{
[OracleArrayMapping()]
public Int32[] Array;
private OracleUdtStatus[] m_statusArray;
public OracleUdtStatus[] StatusArray
{
get
{
return this.m_statusArray;
}
set
{
this.m_statusArray = value;
}
}
private bool m_bIsNull;
public bool IsNull
{
get
{
return m_bIsNull;
}
}
public static VArray Null
{
get
{
VArray obj = new VArray();
obj.m_bIsNull = true;
return obj;
}
}
public void ToCustomObject(OracleConnection con, IntPtr pUdt)
{
object objectStatusArray = null;
Array = (Int32[])OracleUdt.GetValue(con, pUdt, 0, out objectStatusArray);
m_statusArray = (OracleUdtStatus[])objectStatusArray;
}
public void FromCustomObject(OracleConnection con, IntPtr pUdt)
{
OracleUdt.SetValue(con, pUdt, 0, Array, m_statusArray);
}
public override string ToString()
{
if (m_bIsNull)
return "VArray.Null";
else
{
string rtnstr = String.Empty;
if (m_statusArray[0] == OracleUdtStatus.Null)
rtnstr = "NULL";
else
rtnstr = Array.GetValue(0).ToString();
for (int i = 1; i < m_statusArray.Length; i++)
{
if (m_statusArray[i] == OracleUdtStatus.Null)
rtnstr += "," + "NULL";
else
rtnstr += "," + Array.GetValue(i).ToString();
}
return "VArray(" + rtnstr + ")";
}
}
}
[OracleCustomTypeMapping("SYS.ODCINUMBERLIST")]
public class VArrayFactory : IOracleCustomTypeFactory, IOracleArrayTypeFactory
{
// IOracleCustomTypeFactory
public IOracleCustomType CreateObject()
{
return new VArray();
}
// IOracleArrayTypeFactory Interface
public Array CreateArray(int numElems)
{
return new Int32[numElems];
}
public Array CreateStatusArray(int numElems)
{
// CreateStatusArray may return null if null status information
// is not required.
return new OracleUdtStatus[numElems];
}
}
一般的想法是,与OCI示例类似,您必须将参数转换为SYS.ODCINUMBERLIST(或其他有效类型)。默认情况下,C#OracleDBType中未定义该类型,因此您必须使用UdtTypeName和自定义Factory / Class进行成功绑定。
这是受following post on defining custom types启发的。
此精确解决方案仅适用于INT / NUMBER值,因为它是从SYS.ODCINUMBERLIST表类型中捎带的。如果其他类型需要它,您可能需要查找/写入其他自定义表类型。