检查主题的多个先决条件

时间:2019-05-04 16:11:36

标签: mysql sql database vb.net phpmyadmin

我的朋友告诉我,我需要将必备组件ID设置为varchar并存储必备组件,并用逗号分隔吗?

这就是我现在所拥有的。 我设法根据主题ID获取并检查必备ID 但是我只能存储和检查一个先决条件,因为我将其存储为整数,但是现在我将其更改为varchar并用逗号分隔了另一个先决条件。查看我的数据库

enter image description here

这就是我现在所拥有的。 该代码仅适用于一个先决条件。

 Private Sub EnrollStudent(ByVal StudentID As Integer, ByVal SubjectID As Integer)
    '**CHECKING IF SUBJECT HAS A PREREQUISITE
    Dim Prerequisite As Integer
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select pre_id from subject  where subject_id = @subject_id", cn)
            cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
            cn.Open()
            Prerequisite = CInt(cmd.ExecuteScalar)
        End Using
    End Using
    '**ANSWER = NO / NO PREREQUISITE ? DO THIS -> CALL THE INSERTENROLLMENT FUNCTION 
    If Prerequisite = 0 Then
        InsertEnrollment(StudentID, SubjectID, lbl_As_SchoolYear.Text)
        Return
    End If

    '**ANSWER = YES / THERE IS A PREREQUISITE
    '**CHECKING IF THE SUBJECT IS PASSED

    '**CHECKING FOR INC GRADES SINCE INC IS STRING
    Dim PassingGradeStr As String
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select grade FROM student_subject WHERE sub_id =@sub_id AND student_id =@student_id", cn)
            cmd.Parameters.Add("@sub_id", MySqlDbType.Int32).Value = Prerequisite
            cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
            cn.Open()
            PassingGradeStr = CStr(cmd.ExecuteScalar)
        End Using
    End Using

    '**CHECKING IF THE GRADE IS INC / IF INC THEN EXIT SUB
    If PassingGradeStr = "INC" Then
        MessageBox.Show("GRADE STILL INC")
        Exit Sub
    End If

    '**PASSING GRADE IS NOT INC / SO GRADE MUST BE INTEGER

    Dim PassingGrade As Integer
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select Grade From student_subject Where sub_id = @sub_id And student_id = @student_id", cn)
            cmd.Parameters.Add("@sub_id", MySqlDbType.Int32).Value = Prerequisite
            cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
            cn.Open()
            PassingGrade = CInt(cmd.ExecuteScalar)
        End Using
    End Using

    '**CHECKING IF THE GRADE IS GREATER THAN 0 / IF TRUE THEN IT IS PASSED / CALL INSERTENROLLMENT FUNCTION
    If PassingGrade > 0 Then
        If PassingGrade = 4 Then
            MessageBox.Show("Prerequisite Subject is 4")
            Exit Sub
        End If

        If PassingGrade = 5 Then
            MessageBox.Show("Prerequisite Subject is Failed")
            Exit Sub
        End If
        InsertEnrollment(StudentID, SubjectID, lbl_As_SchoolYear.Text)
    Else
        MessageBox.Show("Student cannot enroll because of prerequisite.")
    End If
End Sub

我通过做评论建议之一来做到这一点。通过为前提条件添加另一列。,我知道这并不是真正有效的方法,因此任何建议我都乐意尝试。谢谢。

我将张贴我为修复我所做的事情。 更新enter image description here

有点混乱,因为我重复了我必须检查的所有代码 为我添加的另一列。

  '**CHECKING IF SUBJECT HAS A PREREQUISITE
    Dim Prerequisite As Integer
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select pre_id from subject  where subject_id = @subject_id", cn)
            cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
            cn.Open()
            Prerequisite = CInt(cmd.ExecuteScalar)
        End Using
    End Using
    '**ANSWER = NO / NO PREREQUISITE ? DO THIS -> CALL THE INSERTENROLLMENT FUNCTION 


    Dim Prerequisite2 As Integer
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select pre_id2 from subject  where subject_id = @subject_id", cn)
            cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
            cn.Open()
            Prerequisite2 = CInt(cmd.ExecuteScalar)
        End Using
    End Using



    If Prerequisite = 0 Then
        InsertEnrollment(StudentID, SubjectID, lbl_As_SchoolYear.Text)
        Return
    End If


    If Prerequisite2 = 0 Then
        InsertEnrollment(StudentID, SubjectID, lbl_As_SchoolYear.Text)
        Return
    End If




    '**ANSWER = YES / THERE IS A PREREQUISITE
    '**CHECKING IF THE SUBJECT IS PASSED

    '**CHECKING FOR INC GRADES SINCE INC IS STRING
    Dim PassingGradeStr As String
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select grade FROM student_subject WHERE sub_id =@sub_id AND student_id =@student_id", cn)
            cmd.Parameters.Add("@sub_id", MySqlDbType.Int32).Value = Prerequisite
            cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
            cn.Open()
            PassingGradeStr = CStr(cmd.ExecuteScalar)
        End Using
    End Using


    Dim PassingGradeStr2 As String
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select grade FROM student_subject WHERE sub_id =@sub_id AND student_id =@student_id", cn)
            cmd.Parameters.Add("@sub_id", MySqlDbType.Int32).Value = Prerequisite2
            cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
            cn.Open()
            PassingGradeStr2 = CStr(cmd.ExecuteScalar)
        End Using
    End Using






    '**CHECKING IF THE GRADE IS INC / IF INC THEN EXIT SUB
    If PassingGradeStr = "INC" Then
        MessageBox.Show("GRADE STILL INC")
        Exit Sub
    End If

    If PassingGradeStr2 = "INC" Then
        MessageBox.Show("GRADE STILL INC")
        Exit Sub
    End If



    '**PASSING GRADE IS NOT INC / SO GRADE MUST BE INTEGER

    Dim PassingGrade As Integer
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select Grade From student_subject Where sub_id = @sub_id And student_id = @student_id", cn)
            cmd.Parameters.Add("@sub_id", MySqlDbType.Int32).Value = Prerequisite
            cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
            cn.Open()
            PassingGrade = CInt(cmd.ExecuteScalar)
        End Using
    End Using


    Dim PassingGrade2 As Integer
    Using cn As New MySqlConnection(connstring)
        Using cmd As New MySqlCommand("Select Grade From student_subject Where sub_id = @sub_id And student_id = @student_id", cn)
            cmd.Parameters.Add("@sub_id", MySqlDbType.Int32).Value = Prerequisite2
            cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
            cn.Open()
            PassingGrade2 = CInt(cmd.ExecuteScalar)
        End Using
    End Using




    '**CHECKING IF THE GRADE IS GREATER THAN 0 / IF TRUE THEN IT IS PASSED / CALL INSERTENROLLMENT FUNCTION
    If PassingGrade > 0 And PassingGrade2 > 0 Then
        If PassingGrade = 4 Or PassingGrade2 = 4 Then
            MessageBox.Show("Prerequisite Subject is 4")
            Exit Sub
        End If

        If PassingGrade = 5 Or PassingGrade2 = 5 Then
            MessageBox.Show("Prerequisite Subject is Failed")
            Exit Sub
        End If
        InsertEnrollment(StudentID, SubjectID, lbl_As_SchoolYear.Text)
    Else
        MessageBox.Show("Student cannot enroll because of prerequisite.")
    End If

0 个答案:

没有答案