嗨,我正在使用ODP.NET(C#)将对象传递给oracle包中的过程。由于关联数组,我正在使用ODP.NET。我无法在ODP.NET中找到oracledbtype.object来使参数oracledbtype成为对象。我必须使用ODP.NET在.NET端使用哪个dbtype作为参数。

         public Oracle.DataAccess.Client.OracleCommand oc = new Oracle.DataAccess.Client.OracleCommand();



        oc.Parameters.Add("Param1", OracleDbType.Varchar2).Value = txt_RequestId.Text;

        //assign the array to the parm
        Oracle.DataAccess.Client.OracleParameter additionalBusiness_AssocParm = new Oracle.DataAccess.Client.OracleParameter();

        // When I tried to assign parameter OracleDbType to Varchar2 it is generating an error [PLS-00306: wrong number or types of arguments in call to 'INSERT_REQUEST'].
        // When I tried to change that value to Object. It is giving compliation error that object dbtype doesn't exist in Oracle.Data.Client.OracleDbType
        additionalBusiness_AssocParm.OracleDbType = Oracle.DataAccess.Client.OracleDbType.Varchar2;
        additionalBusiness_AssocParm.CollectionType = Oracle.DataAccess.Client.OracleCollectionType.PLSQLAssociativeArray;
        additionalBusiness_AssocParm.Direction = ParameterDirection.Input;
        additionalBusiness_AssocParm.Value = unitId; // unitId is an array of User defined type
        oc.Parameters.Add(additionalBusiness_AssocParm);

        try
        {
            DoQueryWithODP("TEST_PKG.INSERT_UNIT", true);
        }

最佳答案

好的,在没有您的pl / sql规范的情况下,我将尝试解决此问题,但我们将了解其工作原理。

我将使用ODP安装中提供的示例@%ORA_HOME%\ odp.net \ samples \ 2.x \ AssocArray \ AssocArray.sln
作为示例谈话要点。
但这个网站也有帮助
Using PL/SQL Associative Arrays(实际上是mservidio提供的文章中的链接,尽管该文章是关于ArrayBind而不是关联数组的)



CREATE TABLE TestAssociativeArray(COL1 varchar2(20), COL2 varchar2(20)) ;
/
create or replace PACKAGE MyTestAssociativeArray AS
   TYPE AssocArrayVarchar2_t is table of VARCHAR(20) index by BINARY_INTEGER;
    PROCEDURE TestVarchar2(Param1 IN TestAssociativeArray.COL1%type ,
                           Param2 IN AssocArrayVarchar2_t);
END MyTestAssociativeArray ;
/
create or replace package body MyTestAssociativeArray as
    PROCEDURE TestVarchar2(Param1 IN TestAssociativeArray.COL1%type ,
                           Param2 IN AssocArrayVarchar2_t)
    AS
    i INTEGER ;
    BEGIN
        FOR i in Param2.first..Param2.last LOOP
            insert into TestAssociativeArray(col1, col2)
                        values (Param1 , Param2(i)) ;
        END LOOP ;
    END TestVarchar2 ;
END MyTestAssociativeArray  ;
/




现在为.net代码:

static void Main(string[] args)
{
  // Connect
  string connectStr = getConnection();

  // Setup the Tables for sample

  OracleConnection connection = new OracleConnection(connectStr);
  OracleCommand cmd = new OracleCommand("MyTestAssociativeArray.TestVarchar2", connection);
  cmd.CommandType = CommandType.StoredProcedure ;

  OracleParameter param1 = cmd.Parameters.Add("param1", OracleDbType.Varchar2);
  OracleParameter param2 = cmd.Parameters.Add("param2", OracleDbType.Varchar2);

  // Setup the direction
  param1.Direction = ParameterDirection.Input;
  param2.Direction = ParameterDirection.Input;

  // Specify that we are binding PL/SQL Associative Array
  param2.CollectionType = OracleCollectionType.PLSQLAssociativeArray;

  param1.Value = "ConstantValue" ;
  param2.Value = new string[3]{"Val1",
                               "Val2",
                               "Val3"};


  try
  {
    connection.Open();
    cmd.ExecuteNonQuery();
  }
  catch (Exception e)
  {
    Console.WriteLine(e.Message);
  }

}




和结果(清理)

select * from TestAssociativeArray
/
COL1                 COL2
-------------------- --------------------
ConstantValue        Val1
ConstantValue        Val2
ConstantValue        Val3
/** --clean up
drop package MyTestAssociativeArray;
drop table TestAssociativeArray ;
**/


但是,由于您尚未发布规格,因此可能很简单:
需要:

cmd.BindByName = true;


since ODP by default binds by position AND not by name(这使很多人绊倒)

关于c# - 使用ODP.NET(C#)将对象传递给oracle包中的过程时,必须使用哪种db类型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6917638/

10-09 18:06