2011-08-22 72 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