所以我使用了這個舊的代碼,它爲數據庫調用運行簡單的ExecuteNonQuery命令。我正在使用DbConnection,DbTransaction和其他System.Data.Common命令。這個數據庫類的執行查詢函數有什麼問題嗎?
只要我在項目的某些部分使用該函數,似乎會得到很多空引用錯誤,儘管在其他部分看起來很好。我認爲它與手動打開連接或調用它有一些問題有關,但是我想知道函數本身是否原本設計得很差(不應該有辦法以它被調用的方式修復任何問題嗎?)
我覺得涉及到事務時,這些空引用錯誤會出現得更多,我認爲我得到的錯誤是空例外「_command = _db.GetStoredProcCommand(storedProcedure);」裏面有以下功能。但該存儲過程確實存在,所以它沒有任何意義。
public List<OutputParameter> execute(String storedProcedure, StoredProcedureParameter[] sqlParameters)
{
try
{
List<OutputParameter> outputParameters = new List<OutputParameter>();
_command = _db.GetStoredProcCommand(storedProcedure);
for (int x = 0; x < sqlParameters.GetLength(0); x++)
{
if (sqlParameters[x] != null)
{
StoredProcedureParameter sqlParameter = sqlParameters[x];
String param = sqlParameter.ParameterName;
DbType dbType = sqlParameter.DbType;
object value = sqlParameter.Value;
if (sqlParameter.IsOutputParam)
{
_db.AddOutParameter(_command, param, dbType, 32);
OutputParameter outputParameter = new OutputParameter(param);
outputParameters.Add(outputParameter);
}
else
_db.AddInParameter(_command, param, dbType, value);
}
}
if (_transaction == null)
_db.ExecuteNonQuery(_command);
else
_db.ExecuteNonQuery(_command, _transaction);
foreach (OutputParameter op in outputParameters)
{
op.ParameterValue = _db.GetParameterValue(_command, op.ParameterName);
}
return outputParameters;
}
catch (SqlException sqle)
{
throw new DataAccessException(sqle.ToString());
}
catch (Exception e)
{
throw new DataAccessException(e.ToString());
}
}
'_command'定義在哪裏?還有哪些地方被訪問? – Oded
private DbCommand _command = null; ---這不是呃,除了通過這個GetStoredProcCommand的東西來啓動。它可以在QueryDatabase()等其他函數中訪問,它可以返回結果而不是像execute()那樣執行。 – Dexter
並且您訪問了多少個地方?你在代碼中的許多地方使用'execute'嗎? – Oded