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;