SQL数据读取器只读取VB.net中的第一行

时间:2018-06-16 02:33:32

标签: sql-server database vb.net sqldatareader

我有一个包含名为restaurant的表的数据库。该表的列名为" time"和" tableno",并有20行。 我正在使用此代码来读取数据:

    Dim connString As String = "server=DESKTOP-69QA9LH\SQLEXPRESS; database=servicedb; integrated security=true"
    Dim conn As New SqlConnection(connString)
    Dim command As SqlCommand
    Dim reader As SqlDataReader

    conn.Open()
    Dim query As String
    query = "select time,tableno from restaurant "
    command = New SqlCommand(query, conn)
    reader = command.ExecuteReader
    reader.Read()
    Dim d1 As DateTime = ToolStripStatusLabel1.Text
    Dim d2 As DateTime = reader("time")
    Dim diff As Short = DateDiff(DateInterval.Minute, d2, d1)

    If reader("tableno") = "2" AndAlso diff = "5" Then
        Button3.BackColor = Color.LawnGreen
    End If

    If reader("tableno") = "2" AndAlso diff = "10" Then
        Button3.BackColor = Color.LawnGreen
    End If       

    If reader("tableno") = "2" AndAlso diff = "15" Then
        Button3.BackColor = Color.LawnGreen
    End If

    If reader("tableno") = "1" AndAlso diff = "5" Then
        Button1.BackColor = Color.Brown
    End If

    If reader("tableno") = "1" AndAlso diff = "10" Then
        Button1.BackColor = Color.Brown
    End If
    If reader("tableno") = "1" AndAlso diff = "15" Then
        Button1.BackColor = Color.Brown
    End If

它几乎可以工作,但问题是它只读取表格中的第一行。我的意思是,当我单击按钮处理此代码时,按钮仅根据表格的第一行更改背景颜色。

带有' tableno'的行2是第1行,它改变了背景颜色。但是' tableno' 1是第2行,我无法读取此行以更改背景颜色。

如何使其与其他行一起使用?

2 个答案:

答案 0 :(得分:1)

这里有几点......

  1. SqlConnectionSqlCommandSqlDataReaderIDisposable,因此应位于using块中。
  2. 您只处理第一行,因为您没有使用循环来遍历所有行。
  3. 只有一个" Button1"和一个" Button3",所以虽然下面的代码将遍历所有行,但它每次都会更新相同的控件,因此您不会看到任何差异。如果不知道您的屏幕是什么样的,我无法为此提供解决方案。
  4. 你对类型有点模棱两可。例如,"差异"是short;但是你将它与字符串进行比较。在此基础上,我将假设TableNo列为int。尽量确保将字符串与字符串和整数与int进行比较,依此类推。
  5. 考虑为您的变量指定专有名称,而不仅仅是d1d2
  6. DataReader具有强类型方法,用于从列中获取值。
  7. 不要一遍又一遍地从阅读器中取出相同的值,将其存储在本地变量中。
  8. DateDiff返回一个很长的,而不是短的
  9. 我怀疑你想要一个大于或等于,而不是等于,因为你似乎想要显示预订的距离。
  10. 您需要将ToolStripStatusLabel1.Text从字符串转换为DateTime
  11. 例如......

        Dim d1 As DateTime = DateTime.Parse(ToolStripStatusLabel1.Text)
    
        Dim connString As String = "server=DESKTOP-69QA9LH\SQLEXPRESS; database=servicedb; integrated security=true"
        Using conn As New SqlConnection(connString)
            conn.Open()
    
            Dim query As String
            query = "select time,tableno from restaurant "
            Using command As New SqlCommand(query, conn)
                Using reader = command.ExecuteReader
                    While reader.Read()
    
                        REM 0 because its the first column in the query. If you prefer to look it up, you can do reader.GetOrdinal("time")
                        Dim time As DateTime = reader.GetDateTime(0)
                        REM Its hard to tell from the posted code, so Im going to assume tableno is an int32
                        Dim tableNo As Int32 = reader.GetInt32(1)
    
                        Dim diff As Long = DateDiff(DateInterval.Minute, time, d1)
    
                        If tableNo = 2 AndAlso diff >= 5 Then
                            Button3.BackColor = Color.LawnGreen
                        End If
                        If tableNo = 2 AndAlso diff >= 10 Then
                            Button3.BackColor = Color.LawnGreen
                        End If
                        If tableNo = 2 AndAlso diff >= 15 Then
                            Button3.BackColor = Color.LawnGreen
                        End If
                        If tableNo = 1 AndAlso diff >= 5 Then
                            Button1.BackColor = Color.Brown
                        End If
                        If tableNo = 1 AndAlso diff >= 10 Then
                            Button1.BackColor = Color.Brown
                        End If
                        If tableNo = 1 AndAlso diff >= 15 Then
                            Button1.BackColor = Color.Brown
                        End If
                    End While
                End Using
            End Using
        End Using
    

    看到我在那时已达到的目标,我还发生了一些事情:

    1. 似乎tableno和按钮之间存在关系。
    2. 它始终在同一个按钮中使用相同的颜色。你可能希望颜色变化。
    3. 所以这里是循环部分的替代

                      While reader.Read()
                          REM 0 because its the first column in the query. If you prefer to look it up, you can do reader.GetOrdinal("time")
                          Dim time As DateTime = reader.GetDateTime(0)
                          REM Its hard to tell from the posted code, so Im going to assume tableno is an int32
                          Dim tableNo As Int32 = reader.GetInt32(1)
      
                          Dim diff As Long = DateDiff(DateInterval.Minute, time, d1)
      
                          REM Consider turning this group of lines into a method which converts the diff to a color
                          Dim diffColor As Color = Color.Gray REM Default color
                          If diff >= 5 Then
                              diffColor = Color.LawnGreen
                          End If
                          If diff >= 10 Then
                              diffColor = Color.Brown
                          End If
                          If diff >= 15 Then
                              diffColor = Color.Red
                          End If
      
                          If tableNo = 1 Then
                              Button1.BackColor = diffColor
                          End If
                          If tableNo = 2 Then
                              Button3.BackColor = diffColor
                          End If
                      End While
      

答案 1 :(得分:0)

Richarddissimo' answer的帮助下,我最终得到了以下代码。我刚刚将getDatetime中的两件事改为GetValue并且效果很好。

Dim d1 As DateTime = DateTime.Parse(ToolStripStatusLabel1.Text)

Dim connString As String = "server=DESKTOP-69QA9LH\SQLEXPRESS; database=servicedb; integrated security=true"
Using conn As New SqlConnection(connString)
    conn.Open()

    Dim query As String
    query = "select time,tableno from restaurant "
    Using command As New SqlCommand(query, conn)
        Using reader = command.ExecuteReader
            While reader.Read()

                REM 0 because its the first column in the query. If you prefer to look it up, you can do reader.GetOrdinal("time")
                Dim time As DateTime = reader.getvalue(0)
                REM Its hard to tell from the posted code, so Im going to assume tableno is an int32
                Dim tableNo As Int32 = reader.getvalue(1)

                Dim diff As Long = DateDiff(DateInterval.Minute, time, d1)

                If tableNo = 2 AndAlso diff >= 5 Then
                    Button3.BackColor = Color.LawnGreen
                End If
                If tableNo = 2 AndAlso diff >= 10 Then
                    Button3.BackColor = Color.LawnGreen
                End If
                If tableNo = 2 AndAlso diff >= 15 Then
                    Button3.BackColor = Color.LawnGreen
                End If
                If tableNo = 1 AndAlso diff >= 5 Then
                    Button1.BackColor = Color.Brown
                End If
                If tableNo = 1 AndAlso diff >= 10 Then
                    Button1.BackColor = Color.Brown
                End If
                If tableNo = 1 AndAlso diff >= 15 Then
                    Button1.BackColor = Color.Brown
                End If
            End While
        End Using
    End Using
End Using