문자열 필드 username을 기반으로 단일 테이블에서 정수 값을 검색하려고합니다. 저장 프로 시저 및 직접 텍스트를 사용하여 시도했습니다. 저장된 proc을 실행할 때 적절한 반환 값을 얻습니다. 그러나 적절한 결과는 나오지 않습니다. 여기 쿼리에서 단일 값 검색
코드의 두 세트입니다 - 직접 텍스트 -public int GetUserRole(string CUSER)
{
try
{
SQLCON = new SqlConnection(connectionString);
SQLCON.Open();
SQLCommand = new SqlCommand();
SQLCommand.CommandType = CommandType.Text;
SQLCommand.Parameters.Add("USUsername", SqlDbType.VarChar).Value = CUSER;
SQLCommand.CommandText = "SELECT USRole FROM tblUser WHERE USUsername = CUSER";
Int32 USRole = (Int32) SQLCommand.ExecuteScalar();
return USRole;
}
catch
{
HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx", false);
return 0;
}
}
SQL 쿼리 :
ALTER PROCEDURE [dbo].[spGetUserRole]
-- Add the parameters for the stored procedure here
@username VARCHAR(50)
AS
BEGIN
-- Declare the return variable here
DECLARE @USRole as int
-- Add the T-SQL statements to compute the return value here
SELECT @USRole = tblUser.USRole FROM tblUser WHERE USUsername = @username
-- Return the result of the function
RETURN @USRole
END
Int32 USRole = (Int32) SQLCommand.ExecuteScalar(); 오류를 처리하는 대신 캐스팅 할 캐스팅 된 레코드가 0이면 일반 catch를 사용하여 catch에 삼가고 Exception을 반환하고 0을 반환합니다. –