[英]Put data from MySQL to labels in a table in vb.net
我想將每個月的總和放在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
此代碼將填充圖表,但我也想使用相同的查詢填充標簽。
您不應將標簽標記為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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.