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 に答える