【问题标题】:Hotel Booking System (Room Available,(Buttons))酒店预订系统(房间可用,(按钮))
【发布时间】:2017-09-10 11:25:38
【问题描述】:

我正在制作一个酒店预订系统。我有 15 个按钮代表每个房间。一旦选择了日期,如果房间已经预订,那么我需要带有数字的按钮变成红色并且无法选择。但是,我还需要在日期和日期之间显示红色按钮。

这是我到目前为止的代码。我不确定从哪里开始。

 Dim date1 As DateTime = dtpDateIn.Value.Date
    Dim date2 As DateTime = dtpDateOut.Value.Date
    Dim da As OleDbDataAdapter = New OleDbDataAdapter
    Dim BookingFound As String = False 
    MyConn = New OleDbConnection 

    MyConn.ConnectionString = connString 

    MyConn.Open() 

    str1 = ("Select * from BookingInformation where [Date In] >= '" & date1 & "' AND [Date Out] <= '" & date2 & "'") 

    '("SELECT * FROM [BookingInformation] WHERE [Date In] = #" & dtpDateIn.Value.Date & "#") 

    Dim cmd1 As OleDbCommand = New OleDbCommand(str1, MyConn) 
    dr = cmd1.ExecuteReader 

    While dr.Read() 
        BookingFound = True 

        strDateOut = dr("Date Out").ToString 
        strDateIn = dr("Date In").ToString 
        strRoomNumber = dr("Room Number").ToString 

        CmbRooms.Items.Remove(strRoomNumber)
    End While
    MyConn.Close() 
End Sub

【问题讨论】:

  • 你的代码有什么问题?
  • 如果日期在 date-in 和 date-out 之间,那么它是 >= Date-in 和
  • 这是我目前所拥有的。我正在使用组合框对其进行测试,以查看它是否找到要删除的房间号,但仍然无法正常工作。我已经编辑了上面的代码。
  • 好吧,如果我理解正确的话(这里还是有点早),那么您需要稍微扩展一下您的 SQL。您有 4 种情况需要处理,可以在两个 SQL 条件中完成:[d1 或 d2 在日期范围内] 或 [d1 = date-out]。图表会有所帮助,但我认为我不能在评论中做到这一点。 (4 个情况是 2 个重叠,1 个完全在内部,1 个在之前和之后结束。)第一个条件包括前三个情况,第二个条件是最后一个。希望那里有足够的内容可以让您走上正确的道路!

标签: vb.net button


【解决方案1】:

首先,如果您将所有房间按钮收集到一个列表中并按房间号排序,将会更容易一些。理想情况下,按钮名称应类似于 btnRoom01。如果您确保任何一位数字都以零开头,那么我的示例中使用的排序方法将起作用。如果没有,您需要根据您的要求重新编写它。

创建一个表单宽列表 -

Dim roomButtons As New List(Of Button)

这是收集按钮的代码

Private Sub AddButtonsToCollection()
    roomButtons.Clear()
    For Each ctl As Control In Me.Controls
        Dim btn As Button
        If ctl.GetType = GetType(Button) Then
            btn = CType(ctl, Button)
            If btn.Name.Contains("btnRoom") Then
                roomButtons.Add(btn)
                roomButtons = roomButtons.OrderBy(Function(x) x.Name).ToList
            End If
        End If
    Next
End Sub

你的 while 循环应该是这样的

While dr.Read()
    Dim bookingDateIn, bookingDateOut As Date
    BookingFound = True
    Date.TryParse(dr("Date Out").ToString, bookingDateIn)
    Date.TryParse(dr("Date In").ToString, bookingDateIn)
    Dim RoomNumber As Integer = CInt(dr("Room Number").ToString)
    If (bookingDateIn <= date2) And (bookingDateOut >= date1) Then
        roomButtons(RoomNumber - 1).BackColor = Color.Red
        roomButtons(RoomNumber - 1).Text = RoomNumber.ToString & vbCrLf & bookingDateIn.ToShortDateString & vbCrLf & bookingDateOut.ToShortDateString
    End If
End While

【讨论】:

    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2018-08-29
    相关资源
    最近更新 更多