将 MySQL 中的数据放入 vb.net 中的 table 中的标签

Put data from MySQL to labels in a table in vb.net

我想将每个月的总和放在 MySQL 数据库中 table 的标签中,但我不知道将其放在标签 cz 中,我有 lbl1、lbl2、... lbl12.
我的代码:

  connection.Open()

            query = "   SELECT SUM(Amount_income_table), MONTHNAME(Date_income_table) 
                        FROM bacci.income_table
                        where year(Date_income_table)='" & LblYear.Text & "'
                        GROUP BY MONTHNAME(Date_income_table);"
            Comand = New MySqlCommand(query, connection)
            READER = Comand.ExecuteReader
            While READER.Read
                ChartIncomeYear.Series("Incomes").Points.AddXY(READER.GetString("MONTHNAME(Date_income_table)"), READER.GetString("SUM(Amount_income_table)"))
            End While
            connection.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally

            connection.Dispose()
        End Try

此代码将填充图表,但我也想用相同的查询填充标签。

您不应将标签设为 lbl1、lbl2 和 lbl3 等。您应该在 运行 时创建它们。在一个空白项目中尝试此代码。从这个例子中改编你的代码。我喜欢使用对象列表,但你也可以使用数组

Dim LabelList As List(Of Label)


Sub LoadSumLabel()
    LabelList = New List(Of Label)
    For x = 1 To 12
        Dim NewLabel As New Label
        With NewLabel
            .Name = DateAndTime.MonthName(x)
            .Text = "0"
            .AutoSize = True
            .Left = 10
            .Top = 10 + (LabelList.Count * NewLabel.Height)
        End With
        LabelList.Add(NewLabel)
        Me.Controls.Add(LabelList.Item(LabelList.Count - 1))
        AddHandler LabelList.Item(LabelList.Count - 1).Click, AddressOf Label_Click

        'you can create a panel and add you control to it the same way. So if you resize the form you can have the scroll bars if it doesnt fit
        'somepanel.controls(LabelList.Item(LabelList.Count - 1))
    Next
End Sub

Private Sub Label_Click(sender As Object, e As EventArgs)
    Dim thisLabel As Label = DirectCast(sender, Label)
    MsgBox(thisLabel.Name, vbOKOnly, "Result")
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    LoadSumLabel()
End Sub

Sub RunQuery()
    connection.Open()

    query = "   SELECT SUM(Amount_income_table), MONTHNAME(Date_income_table) 
                    FROM bacci.income_table
                    where year(Date_income_table)='" & LblYear.Text & "'
                    GROUP BY MONTHNAME(Date_income_table);"
    Comand = New MySqlCommand(query, connection)
    READER = Comand.ExecuteReader
    While READER.Read
        ChartIncomeYear.Series("Incomes").Points.AddXY(READER.GetString("MONTHNAME(Date_income_table)"), READER.GetString("SUM(Amount_income_table)"))
        LabelList.Find(Function(lb As Label) lb.Name = READER.GetString("MONTHNAME(Date_income_table)")).Text = READER.GetString("SUM(Amount_income_table)")


    End While
    connection.Close()
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    Finally

    connection.Dispose()
    End Try
End Sub

将您的标签命名为 lblJanuary、lblFebruary、lblMarch .... lblDecember。然后您可以使用此代码轻松解决您的问题:

query = "   SELECT SUM(Amount_income_table) as Total, MONTHNAME(Date_income_table) 
            FROM bacci.income_table
            where year(Date_income_table)='" & LblYear.Text & "'
            GROUP BY MONTHNAME(Date_income_table);"
Comand = New MySqlCommand(query, connection)
READER = Comand.ExecuteReader
While READER.Read
    ChartIncomeYear.Series("Incomes").Points.AddXY(READER.GetString("MONTHNAME(Date_income_table)"), READER.GetString("SUM(Amount_income_table)"))
    Me.Controls("lbl" & READER.GetString("MONTHNAME(Date_income_table)")).Text = READER.GetString("Total")
End While
connection.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message)
Finally

    connection.Dispose()
End Try