我希望 datareader 从数据库中的 varchar 列读取数据,并将读取结果存储在字符串变量中
我希望 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()
I want datareader to read data from a column in database which is varchar and the read result to be stored in string variable but it generates an indexoutofrange exception...Basically I want a validation so that if the user enters a booking time which is already in the database then program should generate an error message. Also an error should be generated if entered value is inside the booked time duration. Like if a booking is there on 12/12/2011 at 4:00 pm and the duration is 2 hours then the program should not allow any booking till 6:00 pm unless a booking is for some other game or for some other courtno (in case of badminton, squash, lawn tennis etc)
I'm trying the following code for this:
Dim str2 As String ' defines string variable for taking select query
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 SQL 语句:
您没有选择名为“game”的列,因此显然您无法在数据读取器中引用它。在SQL语句中添加“game”列,假设它存在,这个问题就会消失。当然,您可能还有其他问题。
Your SQL statement:
You're not selecting a column called "game", so obviously you can't reference this in your datareader. Add the "game" column to the SQL statement, assuming it exists, this problem will disappear. You might have other problems of course.