0
public void BeginTransaction()
{
    try
    {
        this._keepalive = true;
        if (_oracleConnection.State != ConnectionState.Open)
            _oracleConnection.Open();
            //_oracleConnection.Autocommit = false;
        this._transaction = this._oracleConnection.BeginTransaction(IsolationLevel.ReadCommitted);
    }
    catch (Exception ex)
    {
        _hasError = true;
        _ErrorMessage = ex.Message + "::" + ex.StackTrace;
    }
}

public void CommitTransaction()
{
    try
    {
        this._transaction.Commit();
        this._keepalive = false;
    }
    catch (Exception ex)
    {
        _hasError = true;
        _ErrorMessage = ex.Message + "::" + ex.StackTrace;
    }
}

public void RollbackTransaction()
{
    try
    {
        this._transaction.Rollback();
        this._keepalive = false;
    }
    catch (Exception ex)
    {
        _hasError = true;
        _ErrorMessage = ex.Message + "::" + ex.StackTrace;
    }
}

public string ExecuteSPNonQuerySingleReturnValue(string storedProcName, object[] parameterValues, string outParameterName, bool useTransaction = false)
{
    _hasError = false; _ErrorMessage = "";
    string result = "";
    try
    {
        if (_oracleConnection.State == ConnectionState.Closed)
            _oracleConnection.Open();
        if (_oracleConnection.State == ConnectionState.Open)
        {
            OracleCommand objOraCommand = new OracleCommand();
            objOraCommand.Connection = _oracleConnection;
            objOraCommand.CommandText = storedProcName;
            objOraCommand.CommandType = CommandType.StoredProcedure;
            if (useTransaction == true)
                objOraCommand.Transaction = this._transaction;
            OracleCommandBuilder.DeriveParameters(objOraCommand);
            for (int i = 0; i < parameterValues.Length; i++)
            {
                //objOraCommand.Parameters.Add(new OracleParameter(parameterNames[i], OracleType.VarChar)).Value = (parameterValues[i] == null) ? DBNull.Value : parameterValues[i];
                // It threw exception over here. Below this line.
                objOraCommand.Parameters[i].Value = (parameterValues[i] == null) ? DBNull.Value : parameterValues[i];
                //objOraCommand.Parameters.AddWithValue(parameterNames[i], (parameterValues[i] == null) ? DBNull.Value : parameterValues[i]);
            }
            objOraCommand.ExecuteNonQuery();
            result = objOraCommand.Parameters[outParameterName].Value.ToString();
        }
    }
    catch (Exception ex)
    {
        _hasError = true;
        _ErrorMessage = ex.Message;
    }
    finally
    {
        if (_oracleConnection.State == ConnectionState.Open && _keepalive == false)
        _oracleConnection.Close();
    }
    return result;
}

我在这条线上遇到了异常。

objOraCommand.Parameters[i].Value = (parameterValues[i] == null) ? DBNull.Value : parameterValues[i];

有谁知道是什么问题?它在没有交易的情况下工作正常。此方法在添加事务后立即开始出错。

我正在使用内置的 .Net oracle 客户端库。

using System.Data.OracleClient;
4

2 回答 2

0

基本上参数中没有参数,您正在尝试访问它的索引,这就是它给出错误的原因。您应该添加像 o 这样的参数,bjOraCommand.Parameters.Add()并且您想尝试访问该值objOraCommand.Parameters[i].Value并首先将参数添加到该位置,就像您在列表和数组中所做的那样。而且这个错误与事务无关,我唯一的建议是正确使用事务,而不是像这个复杂的代码。

于 2016-10-28T13:53:00.707 回答
0

使用objOraCommand.Parameters.Add()而不是objOraCommand.Parameters[i].Value = xxxx. 并且parameterValues应该是OracleParameter类型。更多检查这个页面

于 2016-10-28T13:37:13.570 回答