【发布时间】:2011-08-16 12:12:22
【问题描述】:
我希望 datareader 从数据库中的列读取数据,该列是 varchar,读取结果存储在字符串变量中,但它会生成 indexoutofrange 异常...基本上我想要验证,以便如果用户输入预订时间它已经在数据库中,那么程序应该生成一条错误消息。如果输入的值在预定的持续时间内,也会产生错误。就像如果在 2011 年 12 月 12 日下午 4:00 进行预订并且持续时间为 2 小时,那么该程序不应该在下午 6:00 之前允许任何预订,除非预订是针对其他游戏或其他球场(如果是羽毛球、壁球、草地网球等)
我正在为此尝试以下代码:
Dim str2 As String ' 定义用于选择查询的字符串变量
str2 = "select booking_date, booking_time, booking_duration, poolno, courtno, tableno from Bookings"
Dim a, b As Date
Dim c As Date
Dim d, f, g, l As String
Dim dur As Integer
Dim cmd2 As New SqlCommand(str2, con)
con.Open()
Dim bookchk As SqlDataReader = cmd2.ExecuteReader
While bookchk.Read()
a = bookchk("booking_date")
b = bookchk("booking_time")
c = b.ToLongTimeString
dur = bookchk("booking_duration")
l = bookchk("game") 'exception is generated here
If CmboGame.SelectedItem = "Swimming" Then
If bookchk("poolno") IsNot System.DBNull.Value Then
d = bookchk("poolno")
End If
Else : d = ""
End If
If CmboGame.SelectedItem = "Table Tennis" Then
If bookchk("tableno") IsNot System.DBNull.Value Then
f = bookchk("tableno")
End If
Else : f = ""
End If
If CmboGame.SelectedItem IsNot "Table Tennis" And CmboGame.SelectedItem IsNot "Swimming" Then
If bookchk("courtno") IsNot System.DBNull.Value Then
g = bookchk("courtno")
End If
Else : g = ""
End If
If TxtBookDate.Text = a And TxtBookTime.Text = c And TxtPoolNo.Text = d And TxtCrtNo.Text = g And TxtTblNo.Text = f And CmboGame.SelectedItem = l 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 time, h, i, j, n As DateTime
n = c.AddHours(dur)
time = TxtBookTime.Text
h = time.AddHours(TxtBookDur.Text)
i = time.ToLongTimeString
j = h.ToLongTimeString
If TxtBookDate.Text = a And TxtPoolNo.Text = d And TxtCrtNo.Text = g And TxtTblNo.Text = f And CmboGame.SelectedIndex = l And i > c And i <= n Or j > n 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
End While
bookchk.Close()
con.Close()
【问题讨论】: