我正在为体育俱乐部制作预订系统。主要目的是避免预订中的重叠。我的代码工作正常,只有1个案例存在重叠不可避免的情况。
如果2011年12月27日下午5:00在数据库中预订2小时,我尝试在2011年12月27日下午5点到7点之间进行新预订,那么我的代码会生成错误消息。即使我尝试在下午4点预订2小时,也会产生错误信息,因为第2小时将在5:00到7:00之间与已预订的预订重叠。
现在问题部分来了。当天更改它不会生成错误消息,即如果预订在12/27/2011晚上11:00持续3小时,那么它不应该允许新的预订到12/22/2011凌晨2:00但是当我尝试在凌晨1:00预订12/28/2011,将其保存在数据库中,不会生成错误消息。我希望在这种情况下生成错误消息。
我在数据库中使用两个单独的字段用于时间,一个用于日期。它们都有DateTime数据类型。
这是我的代码:
Dim str2 As String ' defines string variable for taking select query
str2 = "select booking_date, booking_time, booking_duration, game, poolno, courtno, tableno from Bookings"
Dim dbdate, dbtime As Date 'defines variables to store values of date and time after reading from the database
Dim dbonlytime As Date 'defines variable to store just time value after separating it from DateTime value read from the database
Dim dbpoolno, dbtblno, dbcrtno, dbgame As String 'defines variables to store values of poolno, courtno, tableno, game after reading from the database
Dim dbdur As Integer 'defines variable to store duration value after reading from the database
Dim cmd2 As New SqlCommand(str2, con) 'defines a new sql command with str2 as query string and con as connection string
con.Open() 'sets the connection state to open
Dim bookchk As SqlDataReader = cmd2.ExecuteReader 'Defines and initiates the datareader to read data from database using cmd2 command
While bookchk.Read() ' iterates the datareader to read values of booking date, booking time, booking duration, game, poolno, courtno and tableno from the database
dbdate = bookchk("booking_date")
dbtime = bookchk("booking_time")
dbonlytime = dbtime.ToLongTimeString 'Converts the DateTime value read from the database for booking time into LongTime format and stores in dbonlytime variable
dbdur = bookchk("booking_duration")
dbgame = bookchk("game")
If CmboGame.SelectedItem = "Swimming" Then 'if selected item on cmbogame is swimming then reads poolno value from database otherwise puts an empty string in the dbpoolno variable
If bookchk("poolno") IsNot System.DBNull.Value Then
dbpoolno = bookchk("poolno")
End If
Else : dbpoolno = ""
End If
If CmboGame.SelectedItem = "Table Tennis" Then 'if selected item on cmbogame is table tennis then reads tableno value from database otherwise puts an empty string in the dbtblno variable
If bookchk("tableno") IsNot System.DBNull.Value Then
dbtblno = bookchk("tableno")
End If
Else : dbtblno = ""
End If
If CmboGame.SelectedItem IsNot "Table Tennis" And CmboGame.SelectedItem IsNot "Swimming" Then 'if selected item on cmbogame is other than swimming and table tennis then reads courtno value from database otherwise puts an empty string in the dbcrtno variable
If bookchk("courtno") IsNot System.DBNull.Value Then
dbcrtno = bookchk("courtno")
End If
Else : dbcrtno = ""
End If
'checks if the entered values match exactly to any record in the database. If yes then generates an error message
If TxtBookDate.Text = dbdate And TxtBookTime.Text = dbonlytime And TxtPoolNo.Text = dbpoolno And TxtCrtNo.Text = dbcrtno And TxtTblNo.Text = dbtblno And CmboGame.SelectedItem = dbgame 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
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 newtime < addeddbtime And addeddbtime > changetime And addednewtime > dbonlytime And addednewtime < addeddbtime And TxtBookDate.Text = newdate)) Then 'the problem lies here
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
End While
bookchk.Close()
con.Close()