当使用数组调用存储过程一次(而不是调用一个采用原子项的过程并多次调用它)时,必须将 OracleDbType 设置为 Array。该值被设置为在自定义 oracle 数据类型类中指定的基础数据结构化。在这种情况下,它是一个字节[][]。
parameter.OracleDbType = OracleDbType.Array;
parameter.UdtTypeName = "GUIDARRAY";
parameter.Value = arrangedGuidList.ToArray();
// Note, do NOT set the collection type. Default it to "none"
然后我定义了我的 Oracle 自定义类型。有关示例,请参见此处。我改编了 SimpleVarray / SimpleVarrayFactory 类,但让它们与 Byte[][] 一起使用。
%ODAC_HOME%\ODACsamples\odp.net\4\UDT
工厂类的关键部分是自定义类型映射必须匹配参数的 UdtTypeName 属性,并且全部大写。似乎 OracleCustomTypeMapping 导致 ODAC 在运行时将指定的自定义类型绑定到参数中,并通过 ToCustomObject / FromCustomObject 方法在后台某处构建 GuidArray 类。
[OracleCustomTypeMapping("GUIDARRAY")]
public class GuidArrayFactory : IOracleCustomTypeFactory, IOracleArrayTypeFactory
{
public IOracleCustomType CreateObject()
{
return new GuidArray();
}
// IOracleArrayTypeFactory Inteface
public Array CreateArray(int numElems)
{
return new Byte[numElems][];
}
public Array CreateStatusArray(int numElems)
{
// CreateStatusArray may return null if null status information
// is not required.
return new OracleUdtStatus[numElems];
}
}
-
// See %ODAC_HOME%\ODACsamples\odp.net\4\UDT
public class GuidArray : IOracleCustomType, INullable
{
[OracleArrayMapping]
public Byte[][] Array;
private OracleUdtStatus[] m_statusArray;
public OracleUdtStatus[] StatusArray
{
get
{
return m_statusArray;
}
set
{
m_statusArray = value;
}
}
private bool m_bIsNull;
public bool IsNull
{
get
{
return m_bIsNull;
}
}
public static GuidArray Null
{
get
{
GuidArray obj = new GuidArray();
obj.m_bIsNull = true;
return obj;
}
}
public void ToCustomObject(OracleConnection con, IntPtr pUdt)
{
object objectStatusArray = null;
Array = (Byte[][])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 "GuidArray.Null";
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);
}
return "GuidArray(" + rtnstr + ")";
}
}