2011-08-22 51 views
0

如果预订出现在数据库12/27/2011下午5:00 2小时,我努力使新的订票12/27/2011 5之间到下午7点,然后我的代码会生成一条错误消息。即使我尝试在下午4:00预订2小时,也会产生一条错误消息,因为第二个小时将在此处与5:00至7:00之间的预订重叠。问题避免重叠预订我的预约系统应用

现在,这里是问题的一部分。当日更改它不会产生错误信息,即如果预订有12/27/2011在晚上11点3小时,那么它不应该允许新的预订,直到2011年12月28日凌晨2:00但当我尝试预订2011年12月28日凌晨1:00,它将其保存在数据库中,并且不会生成错误消息。我想要在这种情况下生成的错误消息。

我使用一个数据库两个独立的字段的时间和一个日期。他们都有DateTime数据类型。

NEWTIME指上我试图让

addednewtime是指时间的新预订加入持续时间上,我正在努力做出新的预订时间后

addeddbtime只包含时间值(后增加持续时间在数据库预订)从时间字段提取存储在数据库

newdate指的是下一个日期作为一天的变化,在上午12:00,所以如果数据库预订是在晚上11点在12/12/2011年新的日期将有12/13/2011在其中

的问题在于它检查重叠的预订时,预订跨越两天

在这里,如果条件的最后一部分是我的代码:

Dim newtime, addednewtime, addeddbtime, changetime, newdate As DateTime 'defines variables 
addeddbtime = dbonlytime.AddHours(dbdur) 
newtime = TxtBookTime.Text 
addednewtime = newtime.AddHours(TxtBookDur.Text) 



changetime = "12:00:00 AM" 
newdate = dbdate.AddDays(1) 

If TxtBookDate.Text = dbdate And TxtPoolNo.Text = dbpoolno And TxtCrtNo.Text = dbcrtno And TxtTblNo.Text = dbtblno And CmboGame.SelectedItem = dbgame And ((newtime > dbonlytime And newtime < addeddbtime) Or (addednewtime > dbonlytime And addednewtime < addeddbtime) Or (newtime < dbonlytime And addeddbtime > changetime And addednewtime < dbonlytime And addednewtime <= addeddbtime And TxtBookDate.Text = newdate)) Then 
MessageBox.Show("The date and time you have entered has already been booked " & vbCrLf & " Try Again!", "Bookings Overlapped", MessageBoxButtons.OK, MessageBoxIcon.Stop) 

Exit Sub 

End If 

回答

1

想你想要做的是有一个例程检查当前预订和现有预订之间的重叠。你应该有这样的方法,用下面的输入值:

  1. 目前的预订日期现有订单的
  2. 列表

比方说,你有一个叫舱位它有开始和结束DateTime是否(不需要为日期和时间分隔字段,DateTime对象同时包含这两个字段)。

Public Class Booking 

    Public Sub New(ByVal beginDate As DateTime, ByVal endDate As DateTime) 
     Me.BeginDate = beginDate 
     Me.EndDate = endDate 
    End Sub 

    Public Property BeginDate As Date 

    Public Property EndDate As Date 

    'Other booking properties here 

End Class 

你可以有这样的例程,它检查是否有与现有预订的重叠:

Private Function BookingOverlapping(ByVal booking As Booking, ByVal existingBookings As IEnumerable(Of Booking)) As Boolean 
    For Each existingBooking In existingBookings 
     If booking.BeginDate < existingBooking.EndDate AndAlso booking.EndDate > existingBooking.BeginDate Then 
      Return True 
     End If 
    Next 
    Return False 
End Function 

,那么你会使用的方法是这样的:

' Figure out begin and end DateTimes from user input, 
' then create a booking object. 
Dim currentBooking As New Booking(beginDate, endDate) 

' The GetExistingBookings method would retrieve bookings from the database 
' and add new Booking objects to a list. 
Dim existingBookings As List(Of Booking) = GetExistingBookings() 

If BookingOverlapping(currentBooking, existingBookings) 
    MessageBox.Show("The date and time you have entered has already been booked " & vbCrLf & " Try Again!", "Bookings Overlapped", MessageBoxButtons.OK, MessageBoxIcon.Stop) 
End If