我有一个包含名为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行,我无法读取此行以更改背景颜色。
如何使其与其他行一起使用?
答案 0 :(得分:1)
这里有几点......
SqlConnection
,SqlCommand
和SqlDataReader
为IDisposable
,因此应位于using
块中。short
;但是你将它与字符串进行比较。在此基础上,我将假设TableNo列为int
。尽量确保将字符串与字符串和整数与int进行比较,依此类推。d1
,d2
。例如......
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
看到我在那时已达到的目标,我还发生了一些事情:
所以这里是循环部分的替代
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