当使用ODP将对象传递给oracle包中的过程时,必须使用哪个db类型.Net (c#)



嗨,我正在使用ODP.NET(c#)将一个对象传递给oracle包中的过程。我用的是ODP。因为关联数组。NET。我找不到oracledbtype。对象在ODP。. NET设置参数oracledbtype为object。在使用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.netsamples2.xAssocArrayAssocArray.sln中提供的示例作为一个谈话要点的例子。但是这个网站也很有帮助使用PL/SQL关联数组(这实际上来自mservidio提供的文章中的链接,而那篇文章是关于ArrayBind而不是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  ;
/

现在用于。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;

因为ODP默认是按位置而不是按名称绑定的(这绊倒了很多人)

这应该包含了您需要的所有信息。我以前使用过这篇文章来执行您正在尝试的操作。

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

相关内容

  • 没有找到相关文章

最新更新