将变量放在sql查询和标签名称vb.net中

时间:2015-04-14 04:26:31

标签: vb.net

我在查询字符串和分配标签名称中放置了一个变量。那可能吗?如果是那么为什么我的功能不起作用。

我搜索了me.controls(label_name)并发现它有效,但是为什么它在我的功能中不起作用。

Public Sub tallyRatingA()
    Try
        For n = 1 To 5
            For m = 5 To 1
                Call conn()
                query = "SELECT COUNT(a" & n & ") AS tallycount FROM evaluation where teacher_id = '" & tId & "' AND a" & n & " = " & m
                command = New MySqlCommand(query, connection)
                reader = command.ExecuteReader
                While (reader.Read)
                    If reader.HasRows Then
                        tally_count = reader.GetString("tallycount")
                    Else
                        tally_count = 0
                    End If
                End While
                reader.Close()
                connection.Close()
                Call conn()
                query = "SELECT SUM(a" & n & ") AS tallytotal FROM evaluation where teacher_id = '" & tId & "' AND a" & n & " = " & m
                command = New MySqlCommand(query, connection)
                reader = command.ExecuteReader
                While (reader.Read)
                    If reader.HasRows Then
                        tally_total = reader.GetString("tallytotal")
                        total = total + tally_total
                    Else
                        tally_total = 0
                    End If
                End While
                labelname = "a" & n & "_" & m
                Me.Controls(labelname).Text = tally_count & " ( fx: " & tally_total & " )"
                connection.Close()
                reader.Close()
                tally_total = 0
                tally_total = 0
                total = 0
                tally_count = 0
            Next m
        Next n
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:2)

您应修改循环并添加 IFNULL 以检查总和值。

<强>之前

For m = 5 To 1

query = "SELECT SUM(a" & n & ") AS tallytotal FROM evaluation where teacher_id = '" & tId & "' AND a" & n & " = " & m

<强>后

For m = 5 To 1 step -1

query = "SELECT IFNULL(SUM(a" & n & "),0) AS tallytotal FROM evaluation where teacher_id = '" & tId & "' AND a" & n & " = " & m