此代码是关于查看已发布的咨询时间表。不幸的是,我坚持使用if语句。错误发生在以下行:如果dr(“Lecturer”)= ComboBox2.SelectedItem则||我正在使用组合框来显示数据库中的数据,这意味着组合框中有一个项目列表,我将选择任何讲师,以便它显示在表单中。我有一个数据库,其中包含已发布的时间表,如图所示。并且存储在此处(数据库)的所有数据实际上都不可用(声明为red = unavailable,lime = available),除了可用的数据未存储到数据库中。所以,我现在正在做的是从数据库中读取并匹配“时间”,“可用”和“讲师”,并将其更改为(红色)到相应的标签中(用石灰色)。
Imports System.Data.OleDb
Public Class viewconsultationschedule
Dim time1 As String = "8.00am-10.00am"
Dim time2 As String = "10.00am-12.00pm"
Dim time3 As String = "12.00pm-2.00pm"
Dim time4 As String = "2.00pm-4.00pm"
Dim time5 As String = "4.00pm-6.00pm"
Dim day1 As String = "Monday"
Dim day2 As String = "Tuesday"
Dim day3 As String = "Wednesday"
Dim day4 As String = "Thursday"
Dim day5 As String = "Friday"
Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=sdpdatabase.accdb;Persist Security Info=False;")
Dim cmd As OleDbCommand
Dim cmd2 As OleDbCommand
Dim dr As OleDbDataReader
Dim sql As String
Dim sql2 As String
Private Sub ToolStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
'If (ComboBox2.SelectedIndex = 3) Then
Dim time1 As String = "8.00am-10.00am"
Dim time2 As String = "10.00am-12.00pm"
Dim time3 As String = "12.00pm-2.00pm"
Dim time4 As String = "2.00pm-4.00pm"
Dim time5 As String = "4.00pm-6.00pm"
Dim day1 As String = "Monday"
Dim day2 As String = "Tuesday"
Dim day3 As String = "Wednesday"
Dim day4 As String = "Thursday"
Dim day5 As String = "Friday"
Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=sdpdatabase.accdb;Persist Security Info=False;")
sql = "Select * FROM consultationschedule WHERE Lecturer=@Lecturer" ' And [Time]=@Time AND weekDay=@weekDay "
sql2 = "Select COUNT (*) FROM consultationschedule WHERE Lecturer=@lecturer"
conn.Open()
cmd = New OleDbCommand(sql, conn)
cmd2 = New OleDbCommand(sql2, conn)
cmd.Parameters.AddWithValue("@lecturer", ComboBox2.SelectedItem)
'cmd.Parameters.AddWithValue("@[Time]", time1)
'cmd.Parameters.AddWithValue("@weekDay", day1)
cmd2.Parameters.AddWithValue("@lecturer", ComboBox2.SelectedItem)
'cmd2.Parameters.AddWithValue("@[Time]", time2)
'cmd2.Parameters.AddWithValue("@weekDay", day2)
Dim count As Integer = cmd2.ExecuteScalar()
dr = cmd.ExecuteReader
dr = cmd2.ExecuteReader
If (dr.HasRows) Then
For i = 1 To count
If (i = 1) Then
dr.Read()
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label11.BackColor = Color.Red
Else
Label11.BackColor = Color.Lime
End If
ElseIf (i = 2) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label12.BackColor = Color.Red
Else
Label12.BackColor = Color.Lime
End If
ElseIf (i = 3) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label13.BackColor = Color.Red
Else
Label13.BackColor = Color.Lime
End If
ElseIf (i = 4) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label14.BackColor = Color.Red
Else
Label14.BackColor = Color.Lime
End If
ElseIf (i = 5) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label15.BackColor = Color.Red
Else
Label15.BackColor = Color.Lime
End If
ElseIf (i = 6) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label16.BackColor = Color.Red
Else
Label16.BackColor = Color.Lime
End If
ElseIf (i = 7) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label17.BackColor = Color.Red
Else
Label17.BackColor = Color.Lime
End If
ElseIf (i = 8) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label18.BackColor = Color.Red
Else
Label18.BackColor = Color.Lime
End If
ElseIf (i = 9) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label19.BackColor = Color.Red
Else
Label19.BackColor = Color.Lime
End If
ElseIf (i = 10) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label20.BackColor = Color.Red
Else
Label20.BackColor = Color.Lime
End If
ElseIf (i = 11) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label21.BackColor = Color.Red
Else
Label21.BackColor = Color.Lime
End If
ElseIf (i = 12) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label22.BackColor = Color.Red
Else
Label22.BackColor = Color.Lime
End If
ElseIf (i = 13) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label23.BackColor = Color.Red
Else
Label23.BackColor = Color.Lime
End If
ElseIf (i = 14) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label24.BackColor = Color.Red
Else
Label24.BackColor = Color.Lime
End If
ElseIf (i = 15) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label25.BackColor = Color.Red
Else
Label25.BackColor = Color.Lime
End If
ElseIf (i = 16) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label26.BackColor = Color.Red
Else
Label26.BackColor = Color.Lime
End If
ElseIf (i = 17) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label27.BackColor = Color.Red
Else
Label27.BackColor = Color.Lime
End If
ElseIf (i = 18) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label28.BackColor = Color.Red
Else
Label28.BackColor = Color.Lime
End If
ElseIf (i = 19) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label29.BackColor = Color.Red
Else
Label29.BackColor = Color.Lime
End If
ElseIf (i = 20) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label30.BackColor = Color.Red
Else
Label30.BackColor = Color.Lime
End If
ElseIf (i = 21) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label33.BackColor = Color.Red
Else
Label33.BackColor = Color.Lime
End If
ElseIf (i = 22) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label34.BackColor = Color.Red
Else
Label34.BackColor = Color.Lime
End If
ElseIf (i = 23) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label35.BackColor = Color.Red
Else
Label35.BackColor = Color.Lime
End If
ElseIf (i = 24) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label36.BackColor = Color.Red
Else
Label36.BackColor = Color.Lime
End If
ElseIf (i = 25) Then
If dr("Lecturer") = ComboBox2.SelectedItem Then
If dr("Available") = "1" Then
Label37.BackColor = Color.Red
Else
Label37.BackColor = Color.Lime
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
Next
End If
答案 0 :(得分:2)
您从第一个SQL命令中选择记录,然后立即丢弃它们并将其替换为第二个SQL命令的结果:
dr = cmd.ExecuteReader
dr = cmd2.ExecuteReader
第二个命令没有名为"Lecturer"
的字段,它只是一个COUNT()
值。
看起来你想要的只是完全删除这一行:
dr = cmd2.ExecuteReader
因为您已在上面执行cmd2
并将该单个值存储在count
变量中。
另请注意,您的循环不是必需的:
For i = 1 To count
If (i = 1) Then
'...
End If
Next
这个循环在设计上只会迭代一次。因此,您首先不需要循环,只需根据需要执行一次代码。