【问题标题】:Pass value of user-defined type as input parameter to stored procedure in Oracle from C#从 C# 将用户定义类型的值作为输入参数传递给 Oracle 中的存储过程
【发布时间】:2018-07-02 04:27:35
【问题描述】:

我遇到了一个问题,我无法将值对象从 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();
    }
}

我收到一个错误

参数绑定参数名无效

请帮我解决这个问题。提前致谢!

【问题讨论】:

    标签: c# sql oracle


    【解决方案1】:

    我遇到了类似的问题,但我正在从 Oracle 高级队列中读取自定义对象。为了解决我的问题,我创建了一个 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-02
      • 2010-11-02
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多