그래서이 오래된 코드를 사용하여 데이터베이스 호출에 대해 간단한 ExecuteNonQuery 명령을 실행합니다. DbConnection, DbTransaction 및 다른 System.Data.Common 명령을 사용하고 있습니다.이 데이터베이스 클래스의 쿼리 실행 함수에 문제가 있습니까?
다른 부분에서는 잘 보이지만 프로젝트의 특정 부분에서이 기능을 사용할 때마다 Null 참조 오류가 많이 발생하는 것처럼 보입니다. 나는 수동으로 연결을 열거 나 호출하는 것과 관련이 있다고 생각하지만, 함수 자체가 잘못 설계된 것인지 궁금하다. (호출되는 방식으로 문제를 해결할 수있는 방법이 없어야 하는가?)
트랜잭션이 관련되어있을 때 이러한 null 참조 오류가 더 자주 발생한다고 생각합니다. 오류가 발생하는 경우 null 예외가 발생합니다. "_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
개인 DbCommand _command = null; ---이 GetStoredProcCommand 물건을 제외하고 시작된 것은 아닙니다. QueryDatabase() 같은 다른 함수에서 액세스합니다. execute()와 같이 실행하는 대신 결과를 가져옵니다. – Dexter
그리고 몇 군데에 액세스하고 있습니까? 코드의 여러 부분에서'execute'를 사용하고 있습니까? – Oded