2017-10-29 115 views
1

我想從MySQL數據庫中的表中標籤中放置每個月的總和,但我不知道它在標籤中cz我有lbl1,lbl2, ... lbl12。
我的代碼:把數據從MySQL到vb.net中的表中的標籤

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

該代碼將填補圖表,但我也想用相同的查詢,以填補標籤。

回答

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 
+0

對不起,我改變了一些東西左右 – 2017-10-30 04:54:18

+0

謝謝@mutedDisk這正是我想要的。 – sako

+0

@sako沒有問題芽 – 2017-10-30 13:53:47

1

名稱您的標籤這樣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 
+0

謝謝我設法得到相同的結果,通過使用if .. then..else陳述,但其長。這個更好。 @Md。蘇曼卡比爾 – sako

相關問題