代码之家  ›  专栏  ›  技术社区  ›  sako

在vb中将MySQL中的数据放到表中的标签中。网

  •  1
  • sako  · 技术社区  · 8 年前

    我想把每个月的总和放在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
    

    This is the table where i want the data to be

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

    2 回复  |  直到 8 年前
        1
  •  1
  •   user7452368 user7452368    8 年前

    您不应该将标签设置为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
    
        2
  •  1
  •   Md. Suman Kabir    8 年前

    将标签命名为lblJanuary、lblFebruary、lblMarch。。。。LBL十二月。然后,您可以使用以下代码轻松解决问题:

    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