C#Oracle自定义类型映射

时间:2014-08-27 06:09:24

标签: c# .net oracle visual-studio-2010 user-defined-types

尝试将自定义类型的集合作为Oracle函数的返回值。

我有一个oracle自定义对象T_ORA_PARAM和此类型的集合T_ORA_PARAM_LIST。

我使用Visual Studio ODP.Net向导映射了c#类,生成了自定义类:

[OracleCustomTypeMapping("OBJT.T_ORA_PARAM")]
public class T_ORA_PARAM : INullable, IOracleCustomType, IXmlSerializable {

    private bool m_IsNull;

    private string m_PARAMETER_NAME;

    private string m_PARAMETER_VALUE;

    private decimal m_PARAMETER_TYPE;

    private bool m_PARAMETER_TYPEIsNull;

    public T_ORA_PARAM() {
        // TODO : Add code to initialise the object
        this.m_PARAMETER_TYPEIsNull = true;
    }

    public T_ORA_PARAM(string str) {
        // TODO : Add code to initialise the object based on the given string 
    }

    public virtual bool IsNull {
        get {
            return this.m_IsNull;
        }
    }

    public static T_ORA_PARAM Null {
        get {
            T_ORA_PARAM obj = new T_ORA_PARAM();
            obj.m_IsNull = true;
            return obj;
        }
    }

    [OracleObjectMappingAttribute("PARAMETER_NAME")]
    public string PARAMETER_NAME {
        get {
            return this.m_PARAMETER_NAME;
        }
        set {
            this.m_PARAMETER_NAME = value;
        }
    }

    [OracleObjectMappingAttribute("PARAMETER_VALUE")]
    public string PARAMETER_VALUE {
        get {
            return this.m_PARAMETER_VALUE;
        }
        set {
            this.m_PARAMETER_VALUE = value;
        }
    }

    [OracleObjectMappingAttribute("PARAMETER_TYPE")]
    public decimal PARAMETER_TYPE {
        get {
            return this.m_PARAMETER_TYPE;
        }
        set {
            this.m_PARAMETER_TYPE = value;
        }
    }

    public bool PARAMETER_TYPEIsNull {
        get {
            return this.m_PARAMETER_TYPEIsNull;
        }
        set {
            this.m_PARAMETER_TYPEIsNull = value;
        }
    }

    public virtual void FromCustomObject(OracleConnection con, System.IntPtr pUdt) {
        OracleUdt.SetValue(con, pUdt, "PARAMETER_NAME", this.PARAMETER_NAME);
        OracleUdt.SetValue(con, pUdt, "PARAMETER_VALUE", this.PARAMETER_VALUE);
        if ((PARAMETER_TYPEIsNull == false)) {
            OracleUdt.SetValue(con, pUdt, "PARAMETER_TYPE", this.PARAMETER_TYPE);
        }
    }

    public virtual void ToCustomObject(OracleConnection con, System.IntPtr pUdt) {
        this.PARAMETER_NAME = ((string)(OracleUdt.GetValue(con, pUdt, "PARAMETER_NAME")));
        this.PARAMETER_VALUE = ((string)(OracleUdt.GetValue(con, pUdt, "PARAMETER_VALUE")));
        this.PARAMETER_TYPEIsNull = OracleUdt.IsDBNull(con, pUdt, "PARAMETER_TYPE");
        if ((PARAMETER_TYPEIsNull == false)) {
            this.PARAMETER_TYPE = ((decimal)(OracleUdt.GetValue(con, pUdt, "PARAMETER_TYPE")));
        }
    }

    public virtual void ReadXml(System.Xml.XmlReader reader) {
        // TODO : Read Serialized Xml Data
    }

    public virtual void WriteXml(System.Xml.XmlWriter writer) {
        // TODO : Serialize object to xml data
    }

    public virtual XmlSchema GetSchema() {
        // TODO : Implement GetSchema
        return null;
    }

    public override string ToString() {
        // TODO : Return a string that represents the current object
        return "";
    }

    public static T_ORA_PARAM Parse(string str) {
        // TODO : Add code needed to parse the string and get the object represented by the string
        return new T_ORA_PARAM();
    }
}

// Factory to create an object for the above class
[OracleCustomTypeMappingAttribute("OBJT.T_ORA_PARAM")]
public class T_ORA_PARAMFactory : IOracleCustomTypeFactory {

    public virtual IOracleCustomType CreateObject() {
        T_ORA_PARAM obj = new T_ORA_PARAM();
        return obj;
    }
}

[OracleCustomTypeMapping("OBJT.T_ORA_PARAM_LIST")]
public class T_ORA_PARAM_LIST : INullable, IOracleCustomType, IXmlSerializable {

    private bool m_IsNull;

    private T_ORA_PARAM[] m_T_ORA_PARAM;

    public T_ORA_PARAM_LIST() {
        // TODO : Add code to initialise the object
    }

    public T_ORA_PARAM_LIST(string str) {
        // TODO : Add code to initialise the object based on the given string 
    }

    public virtual bool IsNull {
        get {
            return this.m_IsNull;
        }
    }

    public static T_ORA_PARAM_LIST Null {
        get {
            T_ORA_PARAM_LIST obj = new T_ORA_PARAM_LIST();
            obj.m_IsNull = true;
            return obj;
        }
    }

    [OracleArrayMappingAttribute()]
    public virtual T_ORA_PARAM[] Value {
        get {
            return this.m_T_ORA_PARAM;
        }
        set {
            this.m_T_ORA_PARAM = value;
        }
    }

    public virtual void FromCustomObject(OracleConnection con, System.IntPtr pUdt) {
        OracleUdt.SetValue(con, pUdt, 0, this.m_T_ORA_PARAM);
    }

    public virtual void ToCustomObject(OracleConnection con, System.IntPtr pUdt) {
        this.m_T_ORA_PARAM = ((T_ORA_PARAM[])(OracleUdt.GetValue(con, pUdt, 0)));
    }

    public virtual void ReadXml(System.Xml.XmlReader reader) {
        // TODO : Read Serialized Xml Data
    }

    public virtual void WriteXml(System.Xml.XmlWriter writer) {
        // TODO : Serialize object to xml data
    }

    public virtual XmlSchema GetSchema() {
        // TODO : Implement GetSchema
        return null;
    }

    public override string ToString() {
        // TODO : Return a string that represents the current object
        return "";
    }

    public static T_ORA_PARAM_LIST Parse(string str) {
        // TODO : Add code needed to parse the string and get the object represented by the string
        return new T_ORA_PARAM_LIST();
    }
}

// Factory to create an object for the above class
[OracleCustomTypeMappingAttribute("OBJT.T_ORA_PARAM_LIST")]
public class T_ORA_PARAM_LISTFactory : IOracleCustomTypeFactory, IOracleArrayTypeFactory {

    public virtual IOracleCustomType CreateObject() {
        T_ORA_PARAM_LIST obj = new T_ORA_PARAM_LIST();
        return obj;
    }

    public virtual System.Array CreateArray(int length) {
        T_ORA_PARAM[] collElem = new T_ORA_PARAM[length];
        return collElem;
    }

    public virtual System.Array CreateStatusArray(int length) {
        return null;
    }
}

将此参数添加到调用函数:

var parameter = new OracleParameter
                    {   ParameterName = "param_list",
                        OracleDbType = OracleDbType.Object,
                        Direction = ParameterDirection.ReturnValue,
                        UdtTypeName = "OBJT.T_ORA_PARAM_LIST"
                    };

cmd.Parameters.Add(parameter);

当我调用oracle函数时,获取System.InvalidOperationException

{System.InvalidOperationException: Custom type mapping for 'dataSource='HSS' schemaName='OBJT' typeName='T_ORA_PARAM_LIST'' is not specified or is invalid

顺便说一下,我可以看到带有查询的自定义对象:

select * from user_objects o where lower(o.OBJECT_TYPE) like '%type%'

似乎对象有效。我该如何解决?提前致谢

0 个答案:

没有答案