将原始(32)参数数组传递给ODP.NET中的Oracle存储过程

时间:2013-09-09 20:21:23

标签: c# oracle stored-procedures odp.net sqlparameters

我有一个自定义类型和存储过程,定义如下:

CREATE OR REPLACE TYPE GuidArray is varray(1000) of RAW(32);
/

CREATE OR REPLACE 
PROCEDURE BulkInsertTempItemGuid
(
  ItemGuidList IN GuidArray default null
) AS
BEGIN
  --
  -- procedure body here
  --
END;
/

我试图从c#应用程序中调用Oracle的ODP .NET中的这个存储过程。我似乎非常愿意正确设置我的OracleParameter,因为Oracle告诉我我没有向它发送正确的参数集。我正在设置CollectionType,OracleDbType和Value,如下所示。 parametersOracleParameterarrangedGuidListIEnumerable<Guid>

parameter.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
parameter.OracleDbType = OracleDbType.Raw;
parameter.Value = arrangedGuidList.ToArray();

例外:

ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'BulkInsertTempItemGuid'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

1 个答案:

答案 0 :(得分:2)

使用数组调用存储过程一次(而不是调用接受原子项的过程并多次调用它)时,必须将OracleDbType设置为Array。该值设置为结构化的基础数据,如自定义oracle数据类型中所指定。在这种情况下,它是一个byte [] []。

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 + ")";

        }
    }