使用ODP.NET(C#)将对象传递给oracle包中的过程时必须使用哪种db类型?

时间:2011-08-02 19:42:07

标签: c# c oracle odp.net

您好我正在使用ODP.NET(C#)将对象传递给oracle包中的过程。我使用ODP.NET是因为关联数组。我无法在ODP.NET中找到oracledbtype.object来使参数oracledbtype成为对象。哪个dbtype我必须使用ODP.NET在.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 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);
        }

2 个答案:

答案 0 :(得分:1)

好的,我会尝试在没有你的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(这让很多人感到高兴)

答案 1 :(得分:0)

这应该包含您需要的所有信息。我以前用这篇文章来表达你正在尝试的东西。

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