Count=0的OracleParameterCollection索引0无效
本文关键字:索引 无效 OracleParameterCollection Count | 更新日期: 2023-09-27 18:15:33
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;
用objOraCommand.Parameters.Add()
代替objOraCommand.Parameters[i].Value = xxxx
。parameterValues
应为OracleParameter
型。更多信息请查看
基本上在参数中没有参数,你正试图访问它的索引,这就是为什么它给出错误。你应该添加参数,如0 bjOraCommand.Parameters.Add()
,你想尝试访问objOraCommand.Parameters[i].Value
的值,并首先添加参数到该位置,就像你在列表和数组中做的那样。这个错误与Transaction无关,我唯一的建议是正确使用Transaction,而不是像这样复杂的代码。