0

我通过 Oracle 数据访问非托管程序集将 Oracle 用户定义类型映射到 .Net 自定义类型。

我已关注文章https://www.codeproject.com/Articles/1261430/Calling-Oracles-Stored-Procedures-and-Mapping-User

我已经应用了简单的场景。

  1. 在模式级别创建 Oracle UDT,即
CREATE OR REPLACE  TYPE STUDENT_TYP IS OBJECT (
STUDENT_ID NUMBER,
STUDENT_NAME VARCHAR(20),
ADDRESS VARCHAR(20),
AGE NUMBER,
BIRTH_DATE DATE
);

CREATE OR REPLACE  TYPE STUDENT_TYP_TBL IS TABLE OF STUDENT_TYP;

这是程序GET_ALL_STUDENTS主体

PROCEDURE GET_ALL_STUDENTS(P_STUDENTS OUT STUDENT_TYP_TBL)
AS
BEGIN

  SELECT *
    BULK COLLECT INTO P_STUDENTS   
    FROM 
    (
    SELECT  STUDENT_TYP(s.student_id,
            s.student_name,
            s.address,
            s.age,
            s.birth_date,
            null)
    FROM
    student s) ; 
END;

上面的例子工作正常。以上场景只是为了说明实施中没有问题

  1. 现在我在包中定义了类型,例如
CREATE OR REPLACE PACKAGE STUDENT_PKG 
AS

TYPE  PHONE_TYP_RC IS RECORD (
PHONE_ID  STUDENT_PHONE.PHONE_ID%TYPE,
PHONE_TYPE STUDENT_PHONE.PHONE_TYPE%TYPE,
PHONE_NUMBER STUDENT_PHONE.PHONE_NUMBER%TYPE
);

TYPE PHONE_RC_TBL IS TABLE OF PHONE_TYP_RC;

PROCEDURE GET_ALL_PHONES(P_PHONES_TBL OUT STUDENT_PKG.PHONE_RC_TBL);

END;

以下是 STUDENT_PKG 正文

create or replace PACKAGE BODY STUDENT_PKG 
AS

PROCEDURE GET_ALL_PHONES(P_PHONES_TBL OUT STUDENT_PKG.PHONE_RC_TBL)
IS
BEGIN

SELECT PH.PHONE_ID, PH.PHONE_TYPE, PH.PHONE_NUMBER
 BULK COLLECT INTO P_PHONES_TBL
FROM STUDENT_PHONE PH;

-- END OF GET_ALL_PHONES
END;


--END OF PACKAGE BODY
END;

当我添加包含所有信息的 Oracle 参数时,我得到一个 Oracle 异常

“OCI-22303:类型\“STUDENT_PKG\”。\“PHONE_RC_TBL\”未找到”

我已经用 name 定义了 Oracle 自定义映射STUDENT_PKG.PHONE_RC_TBL

我已经尝试过使用以下名称

  • STUDENT_PKG.PHONE_RC_TBL
  • STUDENT_PKG
  • 电话_RC_TBL

当我执行OracleCommandBuilder.DeriveParameters(command);获取命令文本参数时,我得到以下信息:

Oracle 参数快速观看

当我执行时,我得到以下异常

OCI-22303:类型“KAMI”。未找到“STUDENT_PKG”

这意味着处理包的 udt 到 .Net 类的映射有些棘手。

P_PHONES_TBL这是我对参数的.Net自定义类的定义

public abstract class CustomTypeBase<T> :
     IOracleCustomType, IOracleCustomTypeFactory, INullable where T : CustomTypeBase<T>, new()
{
        private bool _isNull;

        public bool IsNull
        {
            get { return this._isNull; }
        }

        public static T Null
        {
            get { return new T { _isNull = true }; }
        }

        public IOracleCustomType CreateObject()
        {
            return new T();
        }

        public abstract void FromCustomObject(OracleConnection con, IntPtr pUdt);
        public abstract void ToCustomObject(OracleConnection con, IntPtr pUdt);
}

public abstract class CustomCollectionTypeBase<TType, TValue> : CustomTypeBase<TType>,
                   IOracleArrayTypeFactory where TType : CustomTypeBase<TType>, new()
{
        [OracleArrayMapping()]
        public TValue[] Values;

        public override void FromCustomObject(OracleConnection connection, IntPtr pointerUdt)
        {
            OracleUdt.SetValue(connection, pointerUdt, 0, Values);
        }

        public override void ToCustomObject(OracleConnection connection, IntPtr pointerUdt)
        {
            Values = (TValue[])OracleUdt.GetValue(connection, pointerUdt, 0);
        }

        public Array CreateArray(int elementCount)
        {
            return new TValue[elementCount];
        }

        public Array CreateStatusArray(int elementCount)
        {
            return new OracleUdtStatus[elementCount];
        }
}

[OracleCustomTypeMapping("STUDENT_PKG.PHONE_TYP_RC")]
public class PhoneRecord : CustomTypeBase<PhoneRecord>
{
        [OracleObjectMapping("PHONE_ID")]
        public int PhoneId { get; set; }

        [OracleObjectMapping("PHONE_TYPE")]
        public string PhoneTYpe { get; set; }

        [OracleObjectMapping("PHONE_NUMBER")]
        public string PhoneNumber { get; set; }

        public override void FromCustomObject(OracleConnection con, IntPtr pUdt)
        {
            OracleUdt.SetValue(con, pUdt, "PHONE_ID", PhoneId);
            OracleUdt.SetValue(con, pUdt, "PHONE_TYPE", PhoneTYpe);
            OracleUdt.SetValue(con, pUdt, "PHONE_NUMBER", PhoneNumber);
        }

        public override void ToCustomObject(OracleConnection con, IntPtr pUdt)
        {
            PhoneId = (int)OracleUdt.GetValue(con, pUdt, "PHONE_ID");
            PhoneTYpe = OracleUdt.GetValue(con, pUdt, "PHONE_TYPE").ToString();
            PhoneNumber = OracleUdt.GetValue(con, pUdt, "PHONE_NUMBER").ToString();
        }
}

[OracleCustomTypeMapping("STUDENT_PKG.PHONE_RC_TBL")]
public class PhoneTable : CustomCollectionTypeBase<PhoneTable, PhoneRecord>
{
}

这是存储过程调用:

OracleCommand command = new OracleCommand("KAMI.STUDENT_PKG.GET_ALL_PHONES", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;

try
{
            //  OracleCommandBuilder.DeriveParameters(command);
            OracleParameter param = new OracleParameter()
            {
                ParameterName = "P_PHONES_TBL",
                Direction = System.Data.ParameterDirection.Output,
                OracleDbType = OracleDbType.Object,
                OracleDbTypeEx = OracleDbType.Object,
                UdtTypeName = "STUDENT_PKG.PHONE_RC_TBL",
            };
            command.Parameters.Add(param);

            command.ExecuteNonQuery();
}
catch (Exception ex)
{
}
4

0 回答 0