【问题标题】:I want datareader to read data from a column in database which is varchar and the read result to be stored in string variable我希望 datareader 从数据库中的 varchar 列中读取数据,并将读取结果存储在字符串变量中
【发布时间】: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()

【问题讨论】:

    标签: sqldatareader datareader


    【解决方案1】:

    您的 SQL 语句:

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

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

    【讨论】:

    • 谢谢...只是忘记更改查询,因为我对代码进行了更改..非常感谢:-)
    • 任何人都可以帮助我提到避免重复预订的逻辑....如果我尝试预订一些不同的 Courtno/poolno/tblno 或者当我尝试它时,保持时间、日期相同不同的游戏它仍然会产生错误消息
    • 我不确定在同一个问题中提出多个问题是否是个好主意 - 您也没有提供问题的详细信息,因此任何人都很难提供建议。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多