从C#将用户定义类型的值作为输入参数传递给Oracle中的存储过程

时间:2018-07-02 04:27:36

标签: c# sql oracle

我遇到了一个问题,我无法将值对象从C#映射到参数过程Oracle。

在Oracle中,我具有以下结构:

CREATE OR REPLACE TYPE EMPMLOY AS OBJECT 
(
    FNAME VARCHAR2 (20),
    LNAME VARCHAR2 (20)
);

CREATE OR REPLACE PROCEDURE PROC_INSEMPLOY(P1 EMPMLOY)
IS
V_EMPLOY EMPMLOY;
BEGIN
    V_EMPLOY :=  P1;

   INSERT INTO MANAGE_EMPLYEE 
   VALUES (V_EMPLOY.FNAME, V_EMPLOY.LNAME);
END;

在C#中,代码如下:

[OracleCustomTypeMapping("EMPMLOY")]
public class Employ
{
    [OracleObjectMapping("FNAME")]
    public string Fname { get; set; }

    [OracleObjectMapping("LNAME")]
    public string Lname { get; set; }
}

using (var conn = new OracleConnection(cs))
{
    conn.Open();
    using (var cmd = conn.CreateCommand())
    {
        var employ = new Employ
        {
            Fname = "Thuy",
            Lname = "Tran"
        };

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "PROC_INSEMPLOY";

        var param = new OracleParameter
        {
            ParameterName = "P1",
            Direction = ParameterDirection.Input,
            OracleDbType = OracleDbType.Object,
            UdtTypeName = "EMPMLOY",
            Value = employ
        };

        cmd.Parameters.Add(param);
        cmd.ExecuteNonQuery();
    }
}

我收到错误

  

无效的参数绑定参数名称

请帮我解决这个问题。预先感谢!

1 个答案:

答案 0 :(得分:0)

我有类似的问题,但是我正在从Oracle Advanced Queue读取自定义对象。为了解决我的问题,我创建了一个IOracleCustomTypeFactory,如下所示:

[OracleCustomTypeMapping("EMPMLOY")]
public class EmployFactory : IOracleCustomTypeFactory
{
    public IOracleCustomType CreateObject()
    {
        return new Employ();
    }
}

我还需要在Class对象中添加以下内容:

public class Employ : IOracleCustomType, INullable
{
    [OracleObjectMapping("FNAME")]
    public string Fname{ get; set; }

    [OracleObjectMapping("LNAME")]
    public string Lname{ get; set; }

    public void FromCustomObject(OracleConnection con, IntPtr pUdt)
    {
        OracleUdt.SetValue(con, pUdt, "FNAME", this.Fname);
        OracleUdt.SetValue(con, pUdt, "LNAME", this.Lname);
    }

    public void ToCustomObject(OracleConnection con, IntPtr pUdt)
    {
        this.Fname = OracleUdt.GetValue(con, pUdt, "FNAME").ToString();
        this.Lname = OracleUdt.GetValue(con, pUdt, "LNAME").ToString();
    }
}