이 질문을 게시하기 전에 조사를했는데 데이터가 반환되지 않을 때 ExecuteScalar는 System.NullReferenceException을 throw합니다. 그래서 저장된 프로 시저를 "return 1"으로 수정하여 반환 값이 보장됩니다. 그러나, 나는 아직도 NULL 참조 예외를 얻고있다. 나는 내가 데이터베이스를 조회 할 수있는 권한이 알고 있도록 값을 선택할 수 있었다 스칼라를 실행 실행하면ExecuteScalar는 1을 반환하는 저장 프로 시저를 호출 할 때 NullReferenceException을 throw합니다.
SqlCommand sqlCommand = new SqlCommand("SELECT * FROM ATableThatHasValues", conn)
:
그래서 나는 데이터가있는 테이블을 조회 할 수있는 SqlCommand를 사용했습니다. 나는 이것이 내가 놓친 proc 권한 셋팅을 저장하고있는 것으로 의심된다.저는 하루 종일이 문제에 매달 렸으므로 어떤 의견이나 제안도 주시면 감사하겠습니다. :(
내 코드는 다음과 같습니다
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
using (SqlCommand sqlCommand = new SqlCommand("GetSomeValue", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
//sqlCommand.Parameters.Add(new SqlParameter("@Id", this.ID));
//sqlCommand.Parameters.Add(new SqlParameter("@State", 1 /* active */));
byte retValue = (byte)sqlCommand.ExecuteScalar();
return retValue;
}
}
감사
어떤 줄이 예외를 throw합니까? 스택 트레이스는 어디에 있습니까? – asawyer
'ExecuteScalar'의 목적을 이해하고 있습니까? 첫 번째 데이터 행의 첫 번째 열을 반환하고 나머지는 무시됩니다. SELECT와 함께 사용하는 이유가 있습니까? –