开发者

Which db type has to used when passing an object to procedure in a oracle package using ODP.NET (C #)?

开发者 https://www.devze.com 2023-03-25 07:38 出处:网络
Hi I am passing an object to procedure in a oracle package using ODP.NET(C#). I am using ODP.NET because of associative arrays. I am unable to find oracledbtype.object in the ODP.NET to make parameter

Hi I am passing an object to procedure in a oracle package using ODP.NET(C#). I am using ODP.NET because of associative arrays. I am unable to find oracledbtype.object in the ODP.NET to make parameter oracledbtype to object. which dbtype I have to use for parameter in .NET side using ODP.NET.

         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 cal开发者_如何学编程l 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);
        }


okay, I'll try to tackle this one without your pl/sql specification but we'll see how this works.

I will use the sample provided in the ODP installation @ %ORA_HOME%\odp.net\samples\2.x\AssocArray\AssocArray.sln as a sample talking point. but this site is also helpful Using PL/SQL Associative Arrays (this is actually off of a link in the article provided by mservidio, wheras that article was about ArrayBind not Associative Arrays)


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  ;
/

now for the .net code:

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);
  }

}

and the results (with cleanup)

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

However, since you haven't posted your spec, it may be something as simple as: needing:

cmd.BindByName = true;

since ODP by default binds by position AND not by name (this trips up a lot of people)


This should have all the information you need. I've used this article before to perform what you're trying.

http://www.oracle.com/technetwork/issue-archive/2009/09-sep/o59odpnet-085168.html

0

精彩评论

暂无评论...
验证码 换一张
取 消