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;
答案 0 :(得分:0)
使用objOraCommand.Parameters.Add()
代替objOraCommand.Parameters[i].Value = xxxx
。 parameterValues
应为OracleParameter
类型。更多检查page
答案 1 :(得分:0)
基本上参数中没有参数,并且您正在尝试访问其索引,这就是它给出错误的原因。你应该添加像bjOraCommand.Parameters.Add()
这样的参数,你想要尝试访问objOraCommand.Parameters[i].Value
的值,然后首先将参数添加到该位置,就像在list和array中一样。并且这个错误与Transaction没有任何关系,我唯一的建议是正确使用Transaction而不是像这个复杂的代码。