如何知道Reservation/CheckIn程序中可用的时间和日期

How to know the available time and date in Reservation/CheckIn Program

我正在使用 Visual Basic.Net,但我无法了解 Hotel/Resort 预订程序的 reservation/checkin 的可用日期、时间和房间。在下面提供的这段代码中,当我尝试在特定日期和时间预订特定房间,然后重新启动程序时,当我尝试再次保存不可用 room/date/time 时,程序的条件错误地允许保存它再次与时间表冲突。我正在使用视觉基础。谢谢:)

Dim varConflictSched As Boolean = False

        Dim dsCheckIn As New DataSet
        Dim daCheckIn As OdbcDataAdapter = New OdbcDataAdapter("SELECT * FROM tblCheckIn ORDER BY ID", MyConn)
        daCheckIn.Fill(dsCheckIn, "tblCheckIn")

        Dim DateTimeRangeNewIn(Val(Me.txtstaying.Text)) As DateTime
        Dim DateCheckInNew As DateTime = Me.dtpCheckIn.Value

        For a As Integer = 0 To dsCheckIn.Tables("tblCheckIn").Rows.Count - 1
            Dim DateTimeRangeOldIn(dsCheckIn.Tables("tblCheckIn").Rows(a)("Staying")) As DateTime

            REM Check if the room number is in used
            If Me.cbRoomNumber.SelectedItem = dsCheckIn.Tables("tblCheckIn").Rows(a)("RoomNumber").ToString Then

                REM Check if the date and time of the specified room number is in used
                Dim varCheckInDate As DateTime = dsCheckIn.Tables("tblCheckIn").Rows(a)("CheckInDate")

                For b As Integer = 0 To dsCheckIn.Tables("tblCheckIn").Rows(a)("Staying")
                    For c As Integer = 0 To Val(Me.txtstaying.Text)
                        DateTimeRangeOldIn(b) = varCheckInDate.AddDays(b)
                        DateTimeRangeNewIn(c) = DateCheckInNew.AddDays(c)

                        If DateTimeRangeOldIn(b).Date = DateTimeRangeNewIn(c).Date Then
                            If DateDiff(DateInterval.Minute, DateTimeRangeOldIn(b), DateTimeRangeNewIn(c)) <= 0 Then
                                varConflictSched = True
                                Exit For

                            End If

                        End If

                    Next

                Next

            End If

        Next

        If Me.txtAmount.Text = "" Or Me.txtSearch.Text = "" Or Me.txtstaying.Text = "" Or Me.txtTotal.Text = "" Or Me.cbRoomNumber.SelectedIndex = -1 Or Me.cbRoomtype.SelectedIndex = -1 Then
            MessageBox.Show("Required field(s) should not be left blank" & vbCrLf & "Please try again", "NO BLANK SPACE", MessageBoxButtons.OK, MessageBoxIcon.Error)

        ElseIf varConflictSched = True Then
            MessageBox.Show("Can't set schedule with this date." & vbCrLf & "Please insert another date.", "CONFLICT", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Else
            Dim DAdapter As OdbcDataAdapter = New OdbcDataAdapter("SELECT * FROM tblCheckIN ORDER BY ID ", MyConn)
            Dim DSet As New DataSet
            DAdapter.Fill(DSet, "tblCheckIN")
            Dim NewID As Integer = DSet.Tables("tblCheckIN").Rows(DSet.Tables("tblCheckIN").Rows.Count - 1)("ID") + 1

            Dim CommCheckIn As OdbcCommand = New OdbcCommand("INSERT INTO tblCheckIN (ID,CustomerID,Roomtype,RoomNumber,Amount,CheckInDate,Staying)VALUES(" & NewID & ",'" & Me.txtSearch.Text & "','" & Me.cbRoomtype.SelectedItem & "','" & Me.cbRoomNumber.SelectedItem & "','" & Me.txtAmount.Text & "','" & Me.dtpCheckIn.Value & "' , '" & Me.txtstaying.Text & "')", MyConn)

            MyConn.Open()
            CommCheckIn.ExecuteNonQuery()
            MyConn.Close()

            MessageBox.Show("Your Checking-In is succesfully saved. ", "SAVED", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Me.ClearAll()

        End If

您不需要循环代码来查找碰撞。

碰撞发生在:

RequestionStartDate <= EndDate
   And 
RequestEndDate >= StartDate

当然要在上面加上roomnumber,不过很简单

所以在预订按钮上,您的代码将执行此操作:

Dim strSQL As String
Dim rstData As New DataSet
Dim daRead As SqlDataAdapter
strSQL = "select * from tblCheckIn where RoomNumber = @RoomNum " & _
         " AND ( (@CheckIn <= CheckOutDate) AND (@CheckOut >= CheckInDate) )"
Dim MySql As SqlCommand = New SqlCommand(strSQL, GetCon)
MySql.Parameters.Add("@RoomNum", SqlDbType.Int).Value = Me.RoomNumber.Value
MySql.Parameters.Add("@CheckIn", SqlDbType.DateTime).Value = Me.dtpCheckIn.Value
MySql.Parameters.Add("@CheckOut", SqlDbType.DateTime).Value = Me.dtpCheckOut.Value

daRead.SelectCommand = MySql
daRead.Fill(rstData)
If rstData.Tables(0).Rows.Count > 0 Then
    MsgBox("you have a booking collsion")
End If

上面的方法是有效的,因为你从不允许碰撞,上面的方法会return你"over lap"的任何记录。这包括这样的情况:

RequestStart                   RequestEnd
  |                              |
       | exsiting Start/end |

    RequestStart                   RequestEnd
      |                              |
  | exist Start                         Exist end |

RequestStart               RequestEnd
    |                           |
          | exist Start                    Exist end |

事实上,上面的简单查询可以找到任何重叠的任何组合。