1

我在 Visual Studio (C#) 中使用参数执行 Oracle 存储过程时遇到问题。参数是用户选择的数组值。当我尝试执行时,它会抛出“索引超出了数组的范围”。我无法解决问题。

我的代码如下:

public void ExecSPWBulkInsert(string spName, object[] myObjects)
{
    OracleCommand command = new OracleCommand();
    OracleParameter[] discoveredParameters = findSPParameters(spName);
    command.Connection = oraConn;
    command.ArrayBindCount = discoveredParameters.Length ;
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = spName;
    OracleParameter[] param = new OracleParameter[discoveredParameters.Length];
    ArrayList[] parameters = new ArrayList[discoveredParameters.Length];

    for (int i = 0; i < discoveredParameters.Length; i++)
    {
        parameters[i] = new ArrayList();
        foreach (object obj in myObjects)
        {
            foreach (var prop in obj.GetType().GetProperties())
            {
                if (prop.Name == discoveredParameters[i].ParameterName)
                {
                    parameters[i].Add(prop.GetValue(obj));
                }
            }
        }

        param[i] = new OracleParameter();
        param[i].Direction = ParameterDirection.Input;
        param[i].ArrayBindSize = new int[parameters[i].Count];
        param[i].Size = 1000;
        param[i].ParameterName = discoveredParameters[i].ParameterName;
        param[i].OracleDbType = discoveredParameters[i].OracleDbType;
        param[i].Value = parameters[i].ToArray();
        command.Parameters.Add(param[i]);
    }

    try
    {
        oraConn.Open();
        command.Transaction = oraConn.BeginTransaction();
        command.ExecuteNonQuery();
        command.Transaction.Commit();
        oraConn.Close();
    }
    catch (Exception ex)
    {
        throw;
    }
    finally
    {
       oraConn.Close();
    }
}
4

0 回答 0