问题在C#中绑定Oracle数组参数

时间:2016-07-26 20:13:27

标签: c# oracle

我一直在尝试调用一个接受3个不同数组作为参数的过程,但是虽然我已经尝试了很多不同的方法来做到这一点,但我仍然得到了一个例外,说明了数量或类型参数在某处不正确。 你能帮我弄清楚我到底做错了什么吗?

到目前为止,这是我的代码:

 using (var command = new OracleCommand(sql, connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.BindByName = true;
                command.Connection.Open();

                //Bind Client Ids Array to Database Parameter
                var nArrClientId = new OracleParameter("nArrClientId", OracleDbType.Int32)
                {
                    Size = clientIds.Length,
                    ArrayBindSize = new int[clientIds.Length],                        
                    Direction = ParameterDirection.Input,
                    CollectionType = OracleCollectionType.PLSQLAssociativeArray
                };

                for (var index = 0; index < clientIds.Length; index++)
                {
                    nArrClientId.ArrayBindSize[index] = 12;                       
                }
                nArrClientId.Value = clientIds;

                //Bind DocNames Array to Database Parameter
                var vArrDocNum = new OracleParameter("vArrDocNum", OracleDbType.Varchar2)
                {
                    Size = docNums.Length,
                    ArrayBindSize = new int[docNums.Length],                        
                    Direction = ParameterDirection.Input,
                    CollectionType = OracleCollectionType.PLSQLAssociativeArray
                };
                for (var index = 0; index < docNums.Length; index++)
                {
                    vArrDocNum.ArrayBindSize[index] = 20;                        
                }
                vArrDocNum.Value = docNums;

                //Bind Status Array to Database Parameter
                var vArrStatus = new OracleParameter("vArrStatus", OracleDbType.Varchar2)
                {
                    Size = statusArr.Length,
                    ArrayBindSize = new int[statusArr.Length],                       
                    Direction = ParameterDirection.Input,
                    CollectionType = OracleCollectionType.PLSQLAssociativeArray
                };
                for (var index = 0; index < statusArr.Length; index++)
                {
                    vArrStatus.ArrayBindSize[index] = 15;                        
                }
                vArrStatus.Value = statusArr;

                command.Parameters.Add(nArrClientId);
                command.Parameters.Add(vArrDocNum);
                command.Parameters.Add(vArrStatus);                  

                command.ExecuteNonQuery();
                connection.Close();                  
                command.Connection.Close();
            }

这是Oracle上的程序定义

   TYPE tnClientId IS TABLE OF clients.id%TYPE INDEX by pls_integer; -- Number (12,0)
TYPE tvDocNumber IS TABLE OF documents.document_number%TYPE INDEX by pls_integer; --VARCHAR2 (20 BYTES)

TYPE tvStatus IS TABLE OF varchar2(15);


PROCEDURE update_status (nArrClientId   IN tnClientId,
                     vArrDocNum     IN tvDocNumber,
                     vArrStatus     IN tvStatus);

欢迎任何帮助

1 个答案:

答案 0 :(得分:1)

您无法使用Nested table,必须使用关联数组。

即。您无法使用TYPE tvStatus IS TABLE OF varchar2(15);

TYPE tvStatus IS TABLE OF varchar2(15) INDEX by pls_integer;

clientIdsdocNumsstatusArr的类型是什么?它们必须是数组,也许你必须写成vArrDocNum.Value = docNums.ToArray();