调度冲突:只能检测确切时间的冲突

时间:2018-02-24 20:14:47

标签: visual-studio ms-access

这是我在顶点项目中必须面对的最后一个问题,它让我疯狂。

基本上,我必须能够确定在安排主题时是否所有部门/教师/房间都在使用,以避免冲突。

这是我所做的工作,但到目前为止,它只能检测房间何时使用。

我无法弄清楚如何能够阻止时间段之间的时间安排。例如:第一次进入将是7-8:30AM。第二次入场时间为上午7:30至上午9点。如果存在前者,它应该拒绝后者,但我无法弄清楚如何做到这一点。这就是我到目前为止所做的事情。你们会怎么做呢?

Public Function DataInUse() As Boolean
    Dim Temp As Boolean
    Temp = False

    If FacultyInUse() = True Then
        MessageBox.Show("Faculty in use.")
        cboFaculty.Focus()
        DisplayFacultyInUse()
        DisplayLabelConflictForFaculty()
        Temp = True

    ElseIf RoomInUse() = True Then
        MessageBox.Show("Room in use.")
        cboRoom.Focus()
        DisplayRoomInUse()
        DisplayLabelConflictForRoom()
        Temp = True
    End If
    Return Temp
End Function

Public Function FacultyInUse() As Boolean
    Dim com As New OleDbCommand(" Select * from qrySubjectOfferring Where cTimeIn >=#" & cboFrom.Text & "# and cTimeOut <=#" & cboTo.Text & "# and Faculty like'" & cboFaculty.Text & "%' and cDay Like '%" & cboDay.Text & "%'", clsCon.con)
    Dim dr As OleDbDataReader = com.ExecuteReader()
    dr.Read()
    If dr.HasRows Then
        Return True
    Else
        Return False
    End If
End Function

Public Function RoomInUse() As Boolean
    Dim com As New OleDbCommand("Select * from qryRoomAvailability WHERE (cTimeIn <=#" & cboFrom.Text & "# AND cTimeOut >=#" & cboFrom.Text & "# AND Room = '" & cboRoom.Text & "' AND cDay = '" & cboDay.Text & "') OR (cTimeIn <=#" & cboTo.Text & "# AND cTimeOut >=#" & cboTo.Text & "# AND Room = '" & cboRoom.Text & "' AND cDay = '" & cboDay.Text & "') OR (cTimeIn >= #" & cboFrom.Text & "# AND cTimeOut <= #" & cboTo.Text & "# AND Room = '" & cboRoom.Text & "' AND cDay = '" & cboDay.Text & "') ", clsCon.con)
    Dim dr As OleDbDataReader = com.ExecuteReader()
    dr.Read()
    If dr.HasRows Then
        Return True
    Else
        Return False
    End If
End Function

Public Function SubjectAlreadyOffered(sSubject As String) As Boolean
    Dim com As New OleDbCommand("Select * from qrySubjectOfferring Where Subject LIKE '%" & sSubject & "%'", clsCon.con)
    Dim dr As OleDbDataReader = com.ExecuteReader()
    dr.Read()
    If dr.HasRows Then
        Return True
    Else
        Return False
    End If
End Function

1 个答案:

答案 0 :(得分:0)

尝试简化SQL以使用Between子句并消除From或To在现有条目中的所有条目

在您的RoomInUse()函数中

Dim strSQL as String

strSQL = "SELECT * FROM qryRoomAvailability WHERE " & _
    " Room = '" & cboRoom.Text & "' AND cDay = '" & cboDay.Text & "'" & _   
    " AND NOT (#" & cboFrom.Text "# BETWEEN [" & cTimeIn & "] AND [" & cTimeOut & "])" & _
    " AND NOT (#" & cboTo.Text "# BETWEEN [" & cTimeIn & "] AND [" & cTimeOut & "])"

Dim com As New OleDbCommand(strSQL, clsCon.con)