gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:01:16 24 4
gpt4 key购买 nike

您好,我正在使用 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 包中的过程时必须使用哪种数据库类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6917638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com