简体   繁体   English

Oracle.DataAccess(ODP.NET)数组绑定“值不在预期范围内”

[英]Oracle.DataAccess (ODP.NET) Array Binding “Value does not fall within the expected range”

my scenario 我的情景

i'm using ODP.NET oracle provider with c# 3.5, and i am trying to pass an array as parameter for a procedure...like this: 我正在使用带有c#3.5的ODP.NET oracle提供程序,我正在尝试传递一个数组作为一个过程的参数...像这样:

var paramNames = new OracleParameter();
paramNames.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
paramNames.ParameterName = "P_JOB_TITLE";
paramNames.Size = 2;
paramNames.Value =  new string[2]{ "name1", "name1" };
cmd.Parameters.Add(paramNames);

when runtime code goes to paramNames.Value = new string[2]{ "name1", "name1" }; 当运行时代码转到paramNames.Value = new string [2] {“name1”,“name1”}; it catch with this error 它抓住了这个错误

"Value does not fall within the expected range" “价值不在预期范围内”

Can anyone fix it? 任何人都可以解决它?

ADDITIONAL INFO 附加信息

Specifying OracleDbType the error is fixed, but executing give me some errors 指定OracleDbType错误是固定的,但执行会给我一些错误

paramNames.OracleDbType = OracleDbType.Varchar2;

"Unable to cast object of type 'System.String[]' to type 'System.IConvertible'." “无法将'System.String []'类型的对象强制转换为'System.IConvertible'。”

my goal is to do something like this 我的目标是做这样的事情

http://aspalliance.com/621_Using_ODPNET_to_Insert_Multiple_Rows_within_a_Single_Round_Trip.3 http://aspalliance.com/621_Using_ODPNET_to_Insert_Multiple_Rows_within_a_Single_Round_Trip.3

ANOTHER PROBLEM WITH OUT PARAMETER 另外一个没有参数的问题

Inserting an out parameter like this 像这样插入一个out参数

            paramNames = new OracleParameter();
            paramNames.ParameterName = "O_JOB_ID";
            paramNames.Size = 3;
            paramNames.Direction = ParameterDirection.Output;
            paramNames.OracleDbType = OracleDbType.Int32;
            paramNames.Value = new int[3] { 0, 0, 0 }; ;
            cmd.Parameters.Add(paramNames);

it is correctly filled when ExecuteNonQuery finished. 当ExecuteNonQuery完成时,它被正确填充。 For example the pls-sql procedure performs 3 inserts and i return the row-id of each array record. 例如,pls-sql过程执行3次插入,并返回每个数组记录的row-id。

But i something goes wrong, for example isnerting the 2nd row, the entire OUT parameters (array) are always set on 0. I expected at least the params[0].value was enhanced 但是我出了点问题,例如在第二行有效,整个OUT参数(数组)总是设置为0.我预计至少params [0] .value被增强了

Thanks 谢谢

I think you are trying to merge an Array Bind {simply binding an array to a param to have it execute multi times -- this is how the example in the link you provided did it} with an Associative Array {re: PLSQLAssociativeArray with an INPUT param of TABLE OF}. 我想你正在尝试合并一个数组绑定 {简单地将一个数组绑定到一个param以使它执行多次 - 这就是你提供的链接中的示例如何做到}}和一个关联数组 {re:PLSQLAssociativeArray with INPUT表格的参数。

Since you didn't post your package/proc that you are running, I am assuming you are doing something like this (just putting this down to validate the assumption) 既然你没有发布你正在运行的包/ proc,我假设你正在做这样的事情(只是把它放下来验证假设)

procedure insertdata(P_JOB_TITLE IN VARCHAR2) as
begin
insert into myTable(x) value (P_JOB_TITLE);
end  insertdata;

To execute this like the author of the article you need to use ArrayBindCount (check out this link, it also has an example) . 要像文章作者那样执行此操作,您需要使用ArrayBindCount(查看此链接,它也有一个示例) This also indicates, if you have multiple parameters, it will expect an ARRAY for each one. 这也表明,如果你有多个参数,它会期望每个参数的ARRAY。

Now to have this executed for all the P_JOB_TITLE() that you pass in 现在为你传入的所有P_JOB_TITLE()执行此操作

//this was missing in your example and MUST be there to tell ODP how many array elements to expect
cmd.ArrayBindCount = 2;

 string[] jobTitleArray = {"name1", "name1"};

OracleParameter paramNames= new OracleParameter("P_JOB_TITLE", OracleDbType.Varchar2);

   //paramNames.CollectionType = OracleCollectionType.PLSQLAssociativeArray;/*once again, you are passing in an array of values to be executed and not a pl-sql table*/

    //paramNames.Size = 2; /* this is unnecessary since it is for a plsql-associative array*/
    paramNames.Value =  jobTitleArray ;
    cmd.Parameters.Add(paramNames);

For a plSQLAssociativeArray example have a look at the samples provided when you installed ODP @ %ORA_HOME%\\odp.net\\samples\\2.x\\AssocArray 对于plSQLAssociativeArray示例,请查看安装ODP时提供的示例@%ORA_HOME%\\ odp.net \\ samples \\ 2.x \\ AssocArray

and for array bind examples (as from the link you provided) @ %ORA_HOME%\\odp.net\\samples\\2.x\\ArrayBind 和数组绑定示例(来自您提供的链接)@%ORA_HOME%\\ odp.net \\ samples \\ 2.x \\ ArrayBind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM