如何使用sqlreader从VB.NET中读取SQL数据 [英] How do I read data from SQL in VB.NET using sqlreader
问题描述
我尝试实现以下场景。
当用户点击按钮5时,它从数据库获取表号和时间,然后取决于button1,button2的时差变化颜色。
i还有包含tableno和time列的数据库。
以下代码给我具体演员无效错误。为什么?
谢谢任何帮助
我尝试过:
i try to achieve following scenario.
when user click button 5 its get table number and time from database then depend on time difference change color of button1,button2.
i also have database with tableno and time columns.
following code give me "specific cast is not valid" error.why?
thank you for any help
What I have tried:
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 = New SqlCommand(query, conn)
Using reader = command.ExecuteReader
While reader.Read()
REM
Dim time As DateTime = reader.GetDateTime(0)
REM
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
推荐答案
猜测 - 这就是全部,我们无法访问您的数据 - 有两个地方你可能会收到这个错误:
At a guess - and that's all it can be, we don't have access to your data - there are two places you might get this error:
Dim d1 As DateTime = DateTime.Parse(ToolStripStatusLabel1.Text)
如果用户输入错误的日期。
解决方案:使用 DateTime.TryParse [ ^ ]相反,并向用户报告问题。
或:
If the user enters a bad date.
Solution: use DateTime.TryParse[^] instead, and report problems to teh user.
Or:
Dim time As DateTime = reader.GetDateTime(0)
REM
Dim tableNo As Int32 = reader.GetInt32(1)
如果您的数据库不包含您认为应该包含的内容,或包含两列中的NULL值。
解决方案:检查数据库并确切了解其中的内容。 time
应该是DATETIME列,并且不包含空值。 tableno
应为INT且不包含空值。
If your DB does not contain what you think it should, or contains NULL values in either column.
Solution: Check your DB and see exactly what is there. time
should be a DATETIME column, and contain no nulls. tableno
should be an INT and contain no nulls.
这篇关于如何使用sqlreader从VB.NET中读取SQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!