我没有通过此存储过程得到正确的结果

时间:2018-11-22 09:37:35

标签: c# sql-server

我使用此存储过程来获取创建的测试列表,但是只要我输入新条目(创建新测试),新条目就不会显示在列表上。

但是我在测试表中添加了2个新列,分别称为StartDateTime和EndDateTime

USE [School-OnPremise]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CMS_Test_GetList] 
@SubjectId int,
@ProgramId int,
@ProgramLevelId int,
@AcademicTerm int,
@AcademicSession int,
@AddedBy int,
@EntityStatus int = null

AS
BEGIN
   SET NOCOUNT ON;
select i.*,
  (select Count(*) from TestQuestion where TestId = i.Id) As QuestionNumber,
  (select Count(*) from StudentTests where TestId = i.Id and score is not 
  null) AS ResultsCount
  from Test AS i
  where i.ClassId =COALESCE(@ProgramId,i.ClassId)
  and i.SubjectId = COALESCE(@SubjectId,i.SubjectId)
  and i.Session = COALESCE(@AcademicSession,i.Session)
  and i.AcademicTerm = COALESCE(@AcademicTerm,i.AcademicTerm)   
  and i.AddedBy = COALESCE(@AddedBy,i.AddedBy)  
  and i.EntityStatus = COALESCE(@EntityStatus,EntityStatus)
  order by i.AddedOn desc
END


GO

该表在我执行存储过程时传递了所需的参数,应该获取属于我传递的参数类别的条目。单击sql表以查看下图

sql table

当我尝试执行存储过程时,这就是我得到的

USE [School-OnPremise]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[CMS_Test_GetList]
        @SubjectId = 3142,
        @ProgramId = 2045,
        @ProgramLevelId = 0,
        @AcademicTerm = 1,
        @AcademicSession = 2018,
        @AddedBy = 3,
        @EntityStatus = null

SELECT  'Return Value' = @return_value

GO

这是我得到的输出

ouput

ouput cont.

2 个答案:

答案 0 :(得分:0)

啊,您的最终条件是测试i.EntityStatus = NULL-这将不起作用-如果列值= null,那么它将仅满足IS NULL运算符。

and (@EntityStatus IS NULL OR (@EntityStatus IS NOT NULL AND i.EntityStatus = @EntityStatus))

答案 1 :(得分:0)

问题不在于测试过程,它来自下面的代码,没有任何东西可以保存enitystatus的值

    public bool insert_New_Test(string TestName, int Duration, int SessionYear, int ClassId, int SubjectId, bool Status, int AddedBy,
                int ProgramLevelId, int AcademicTerm, int questionSetUpType, out int id, DateTime startDateTime, DateTime endDateTime, int? testType)
            {
                bool functionReturnValue = false;
                id = 0;
                try
                {
                    using (SqlConnection con = new SqlConnection(conn))
                    {
                        con.Open();
                        SqlCommand oComm = new SqlCommand();
                        using (oComm)
                        {
                            oComm.Connection = con;
                            oComm.CommandType = CommandType.StoredProcedure;
                            oComm.CommandText = "CMS_Test_Insert";

                            oComm.Parameters.Add(new SqlParameter("@Title", SqlDbType.NVarChar));
                            oComm.Parameters["@Title"].Value = TestName;

                            oComm.Parameters.Add(new SqlParameter("@SubjectId", SqlDbType.Int));
                            oComm.Parameters["@SubjectId"].Value = SubjectId;

                            oComm.Parameters.Add(new SqlParameter("@ClassId", SqlDbType.Int));
                            oComm.Parameters["@ClassId"].Value = ClassId;

                            oComm.Parameters.Add(new SqlParameter("@Session", SqlDbType.Int));
                            oComm.Parameters["@Session"].Value = SessionYear;

                            oComm.Parameters.Add(new SqlParameter("@ProgramLevelId", SqlDbType.Int));
                            oComm.Parameters["@ProgramLevelId"].Value = ProgramLevelId;

                            oComm.Parameters.Add(new SqlParameter("@AcademicTerm", SqlDbType.Int));
                            oComm.Parameters["@AcademicTerm"].Value = AcademicTerm;

                            oComm.Parameters.Add(new SqlParameter("@Duration", SqlDbType.Int));
                            oComm.Parameters["@Duration"].Value = Duration;

                            oComm.Parameters.Add(new SqlParameter("@Status", SqlDbType.Bit));
                            oComm.Parameters["@Status"].Value = Status;

                            oComm.Parameters.Add(new SqlParameter("@AddedBy", SqlDbType.Int));
                            oComm.Parameters["@AddedBy"].Value = AddedBy;


                            oComm.Parameters.Add(new SqlParameter("@QuestionSetupType", SqlDbType.Int));
                            oComm.Parameters["@QuestionSetupType"].Value = questionSetUpType;

                            if (startDateTime > DateTime.MinValue)
                            {
                                oComm.Parameters.Add(new SqlParameter("@StartDateTime", SqlDbType.DateTime));
                                oComm.Parameters["@StartDateTime"].Value = startDateTime;
                            }
                            if (endDateTime > DateTime.MinValue)
                            {
                                oComm.Parameters.Add(new SqlParameter("@EndDateTime", SqlDbType.DateTime));
                                oComm.Parameters["@EndDateTime"].Value = endDateTime;
                            }

                            oComm.Parameters.Add(new SqlParameter("@TestType", SqlDbType.Int));
                            oComm.Parameters["@TestType"].Value = testType;

                            oComm.Parameters.Add(new SqlParameter("@Success", SqlDbType.Bit));
                            oComm.Parameters["@Success"].Direction = ParameterDirection.Output;

                            oComm.Parameters.Add(new SqlParameter("@TestId", SqlDbType.Int));
                            oComm.Parameters["@TestId"].Direction = ParameterDirection.Output;
                            oComm.ExecuteNonQuery();
                            functionReturnValue = Convert.ToBoolean(oComm.Parameters["@Success"].Value);
                            id = Convert.ToInt32(oComm.Parameters["@TestId"].Value);

                        }
                        con.Close();
                    }

                }
                catch (SqlException ex)
                {
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                    functionReturnValue = false;
                }
                return functionReturnValue;

            }

因此,当尝试获取entitystatus的值时,该值为null

public List<TestViewModel> get_List_Of_Test(int SubjectId, int ProgramLevelId, int AcademicTerm, int AcademicSession,
            int ProgramId, int status)
        {
            List<TestViewModel> sList = new List<TestViewModel>();
            try
            {
                using (SqlConnection con = new SqlConnection(conn))
                {
                    con.Open();
                    SqlCommand oComm = new SqlCommand();
                    using (oComm)
                    {
                        oComm.Connection = con;
                        oComm.CommandType = CommandType.StoredProcedure;
                        oComm.CommandText = "CMS_Test_GetList";

                        oComm.Parameters.Add(new SqlParameter("@SubjectId", SqlDbType.Int));
                        oComm.Parameters["@SubjectId"].Value = SubjectId == 0 ? System.Data.SqlTypes.SqlInt32.Null : SubjectId;

                        oComm.Parameters.Add(new SqlParameter("@ProgramId", SqlDbType.Int));
                        oComm.Parameters["@ProgramId"].Value = ProgramId == 0 ? System.Data.SqlTypes.SqlInt32.Null : ProgramId;

                        oComm.Parameters.Add(new SqlParameter("@ProgramLevelId", SqlDbType.Int));
                        oComm.Parameters["@ProgramLevelId"].Value = ProgramLevelId == 0 ? System.Data.SqlTypes.SqlInt32.Null : ProgramLevelId;

                        oComm.Parameters.Add(new SqlParameter("@AcademicSession", SqlDbType.Int));
                        oComm.Parameters["@AcademicSession"].Value = AcademicSession == 0 ? System.Data.SqlTypes.SqlInt32.Null : AcademicSession;

                        oComm.Parameters.Add(new SqlParameter("@AcademicTerm", SqlDbType.Int));
                        oComm.Parameters["@AcademicTerm"].Value = AcademicTerm == 0 ? System.Data.SqlTypes.SqlInt32.Null : AcademicTerm;

                        oComm.Parameters.Add(new SqlParameter("@AddedBy", SqlDbType.Int));
                        oComm.Parameters["@AddedBy"].Value = MembershipHelper.GetActiveUser().Teacher.IsAdmin ? System.Data.SqlTypes.SqlInt32.Null : MembershipHelper.GetActiveUserId;

                        oComm.Parameters.Add(new SqlParameter("@EntityStatus", SqlDbType.Int));
                        oComm.Parameters["@EntityStatus"].Value = status;

                        SqlDataReader rdr = oComm.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            int _sn = 0;
                            while (rdr.Read())
                            {
                                int publicStatus = _rdrHelper.getOrdinalInt32Value(rdr, "EntityStatus");
                                _sn++;
                                TestViewModel qList = new TestViewModel();
                                qList.ProgramLevelId = _rdrHelper.getOrdinalInt32Value(rdr, "ProgramLevel");
                                qList.ProgramLevelText = Extensions.ListEnums.GetProgramLevel().Skip(qList.ProgramLevelId - 1).First().Text;
                                qList.AcademicTermId = _rdrHelper.getOrdinalInt32Value(rdr, "AcademicTerm");
                                qList.AcademicTermText = Extensions.ListEnums.GetTermChoice().Skip(qList.AcademicTermId - 1).First().Text;
                                qList.TestTitle = _rdrHelper.GetOrdinalStringValue(rdr, "Title");
                                qList.ClassId = _rdrHelper.getOrdinalInt32Value(rdr, "ClassId");
                                qList.ClassName = _rdrHelper.GetOrdinalStringValue(rdr, "ClassName");
                                qList.SubjectName = _rdrHelper.GetOrdinalStringValue(rdr, "SubjectName");
                                qList.SessionId = _rdrHelper.getOrdinalInt32Value(rdr, "Session");
                                qList.TestId = _rdrHelper.getOrdinalInt32Value(rdr, "Id");
                                qList.Duration = _rdrHelper.getOrdinalInt32Value(rdr, "Duration");
                                qList.Status = _rdrHelper.getOrdinalBooleanValue(rdr, "Status");
                                qList.QuestionCount = _rdrHelper.getOrdinalInt32Value(rdr, "QuestionNumber");
                                qList.TestSn = _sn;
                                qList.ResultExists = _rdrHelper.getOrdinalInt32Value(rdr, "ResultsCount") > 0;
                                qList.AddedBy = _rdrHelper.getOrdinalInt32Value(rdr, "AddedBy");
                                qList.Test.StartDateTime = _rdrHelper.getNullableOrdinalDatetimeValue(rdr, "StartDateTime");
                                qList.Test.EndDateTime = _rdrHelper.getNullableOrdinalDatetimeValue(rdr, "EndDateTime");
                                qList.Test.EntityStatus = publicStatus;
                                sList.Add(qList);
                            }
                        }
                        con.Close();
                    }

                }
            }

在我的创建页面上,没有任何字段可以传递entitystatus的值

create test page

但是对于编辑页面,有一个字段可以将值传递给entitystatus

edit page

发布状态是提供实体状态值的字段。事情是在创建测试后,我被重定向到编辑页面以单击保存按钮,以便所有条目都将被保存到数据库3中,但是我没有单击保存按钮,我只是返回到显示该列表的索引页面测试。