C#: Pass a user-defined type to a Oracle stored procedure

后端 未结 3 826
盖世英雄少女心
盖世英雄少女心 2020-12-09 22:09

With reference to Oracle: Variable number of parameters to a stored procedure

I have s stored procedure to insert multiple Users into a User table. The table is defi

3条回答
  •  时光取名叫无心
    2020-12-09 22:47

    After many false starts, this post here saved my bacon (binding to a UDT of TABLE OF VARCHAR2(100)).

    Salient points

    • Create a class to hold an Array of of the nested / UDT type (i.e. an Array of string for your varchar2(100))
      • The class must implement the IOracleCustomType and INullable interfaces.
      • It also needs a property to hold the array (i.e. string[]), and the property must be marked with the OracleArrayMapping attribute.
    • Create a second UDT Factory class which implements the IOracleArrayTypeFactory, IOracleCustomTypeFactory interfaces. It needs the following methods
      • CreateObject - creates a new object of the storage class
      • CreateArray - allocates the array of strings to be set in the storage class
      • CreateStatusArray - a status per row is retained
    • The factory class must also be marked with OracleCustomTypeMapping("SCHEMA.UDT_TYPE") where SCHEMA.UDT_TYPE matches your UDT type, viz CREATE TYPE SCHEMA.UDT_TYPE AS TABLE OF VARCHAR2(100)

    By comparison, the bind on the parameter is straightforward:

       var oracleArray = new MyArrayStorageClass
          {
             Array = new string[] {"Hello", "World"}
          };
       command.CommandType = CommandType.StoredProcedure;
       var param = new OracleParameter("ip_parameterName", OracleDbType.Array)
          {
             // Case sensitive match to the `OracleCustomTypeMapping` on the factory
             UdtTypeName = "SCHEMA.UDT_TYPE", 
             Value = oracleArray,
             Direction = ParameterDirection.Input,
          };
       command.Parameters.Add(param);
    

提交回复
热议问题