【问题标题】:Put data from MySQL to labels in a table in vb.net将 MySQL 中的数据放入 vb.net 表中的标签
【发布时间】:2017-10-29 06:10:08
【问题描述】:

我想将每个月的总和放入 MySQL 数据库表中的标签中,但我不知道将其放入标签 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

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

【问题讨论】:

    标签: mysql .net vb.net vb.net-2010


    【解决方案1】:

    您不应该将标签设置为 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
    

    【讨论】:

    • 对不起,我改变了一些东西
    • @sako 没问题 bud
    【解决方案2】:

    将您的标签命名为 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
    

    【讨论】:

    • 谢谢你,我设法通过使用 if.. then..else 语句得到相同的结果,但它很长。这个更好。 @Md。苏曼卡比尔
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多