Oracle儲存過程(Stored Procedure)使用自定義類實體引數
阿新 • • 發佈:2021-01-07
如何在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 indog_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.看一下執行結果,可以看到資料已經插進去了。