Oracle存储过程(Stored Procedure)使用自定义类实体参数

如何在Oracle存储过程使用自定义类实体参数呢?

语言:C#

1.首先新建表、Sequence、存储过程、以及Oracle自定义Type

--新建dog表
create table tbl_dog
(
id number,
DogName nvarchar2(20),
DogColor nvarchar2(20),
DogAge number
);
--为dog表新建sequence为ID使用
create sequence seq_dog
minvalue 1
maxvalue 999999
start with 1
increment by 1
cache 50
cycle;
--新建oracle的type,与C#的Dog类对应
create or replace type dog_type as object(
       DogName nvarchar2(20),
       DogColor nvarchar2(20),
       DogAge nvarchar2(50)
);
--新建oracle的type,存放Dog类型的数组
create or replace type dog_type_array as table of dog_type;
--测试存储过程,主要功能是将传入的dog数组逐条写入数据库
create or replace procedure sp_insert_dog(dogList in dog_type_array) as
begin
  for i in 1 .. dogList.count loop 
    insert into tbl_dog(
                       id,
                       DogName,
                       DogColor,
                       DogAge
                      )
                      values
                      (
                       seq_dog.nextval,
                       dogList(i).DogName,
                       dogList(i).DogColor,
                       dogList(i).DogAge
                      );
  end loop;
end sp_insert_dog;

2.然后我们定于一个Dog类

    [DataContract]
    public class Dog : IOracleCustomType
    {
        [DataMember]
        [OracleObjectMappingAttribute("DOGNAME")]
        public string DogName { get; set; }
        [DataMember]
        [OracleObjectMappingAttribute("DOGCOLOR")]
        public string DogColor { get; set; }
        [DataMember]
        [OracleObjectMappingAttribute("DOGAGE")]
        public Int64 DogAge { get; set; }

        #region IOracleCustomType Members
        public void FromCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
        {
            OracleUdt.SetValue(con, pUdt, "DOGNAME", DogName);
            OracleUdt.SetValue(con, pUdt, "DOGCOLOR", DogColor);
            OracleUdt.SetValue(con, pUdt, "DOGAGE", DogAge);
        }

        public void ToCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
        {
            DogName = (String)OracleUdt.GetValue(con, pUdt, "DOGNAME");
            DogColor = (String)OracleUdt.GetValue(con, pUdt, "DOGCOLOR");
            DogAge = (Int64)OracleUdt.GetValue(con, pUdt, "DOGAGE");
        }

        #endregion
    }

    [OracleCustomTypeMappingAttribute("DOG_TYPE")]
    public class DogFactory : IOracleCustomTypeFactory
    {
        public IOracleCustomType CreateObject()
        {
            return new Dog();
        }
    }

    [OracleCustomTypeMappingAttribute("DOG_TYPE_ARRAY")]
    public class DogListFactory : IOracleArrayTypeFactory
    {
        public Array CreateArray(int numElems)
        {
            return new Dog[numElems];
        }
        public Array CreateStatusArray(int numElems)
        {
            return null;
        }
    }

3.执行存储过程的函数

    public class ExcuteStoreProcedure
    {
        public static int ExecuteDogs(List<Dog> dogList)
        {
            try
            {
                const string conn = @"此处为Oracle链接字符串";
                using (var oc = new Oracle.DataAccess.Client.OracleConnection(conn))
                {
                    oc.Open();
                    Oracle.DataAccess.Client.OracleCommand cmd = oc.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "SP_INSERT_DOG";

                    var p1 = new Oracle.DataAccess.Client.OracleParameter
                    {
                        OracleDbType = Oracle.DataAccess.Client.OracleDbType.Array,
                        Direction = ParameterDirection.Input,
                        UdtTypeName = "DOG_TYPE_ARRAY"
                    };
                    if (dogList != null)
                    {
                        p1.Value = dogList.ToArray();//注意:这里应该是数组
                    }
                    cmd.Parameters.Add(p1);
                    return cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            {
                return -1;
            }
        }
    }

4.执行入口

        private static void Main()
        {
            var dogs = new List<Dog>
            {
                new Dog {DogAge = 1, DogColor = "Red", DogName = "红牛"},
                new Dog {DogAge = 2, DogColor = "Black", DogName = "黑旋风"},
                new Dog {DogAge = 3, DogColor = "Green", DogName = "绿帽子"},
                new Dog {DogAge = 4, DogColor = "White", DogName = "白鹤"},
                new Dog {DogAge = 5, DogColor = "Blue", DogName = "布鲁"},
                new Dog {DogAge = 6, DogColor = "Yellow", DogName = "皇帝"}
            };
            ExcuteStoreProcedure.ExecuteDogs(dogs);
        }

5.看一下执行结果,可以看到数据已经插进去了。

Oracle存储过程(Stored Procedure)使用自定义类实体参数

 

Oracle存储过程(Stored Procedure)使用自定义类实体参数

上一篇:2mysql初始化及多实例案例


下一篇:NodeJs——nvm安装和使用