我希望 datareader 从数据库中的 varchar 列读取数据,并将读取结果存储在字符串变量中

发布于 2024-11-29 23:32:59 字数 2641 浏览 2 评论 0原文

我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2024-12-06 23:32:59

您的 SQL 语句:

str2 = "select booking_date, booking_time, booking_duration, poolno, courtno, tableno from Bookings"

您没有选择名为“game”的列,因此显然您无法在数据读取器中引用它。在SQL语句中添加“game”列,假设它存在,这个问题就会消失。当然,您可能还有其他问题。

Your SQL statement:

str2 = "select booking_date, booking_time, booking_duration, poolno, courtno, tableno from Bookings"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文