我的函数应返回一些值:
Public Function vratiTvrtke(ByVal hwid As String)
Dim hardware As String = hwid
Dim result = New List(Of String)()
Try
ManageConnection(False, konekcija) 'Open connection'
Dim strQuery As String = "SELECT tvrtke_naziv, dabase FROM info.tvrtke;"
Dim SqlCmd As New MySqlCommand(strQuery, dbCon)
Dim reader As MySqlDataReader = SqlCmd.ExecuteReader()
While reader.Read()
End While
reader.Close()
Return result
Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
Return Nothing
Finally
ManageConnection(True, konekcija) 'Close connection
End Try
End Function
从上面的函数返回的数据我应该做一个菜单:
Public Function pripremiSucelje()
Dim barmanager1 As New BarManager
Dim barButtonItem = New BarButtonItem()
barButtonItem.Content = [tvrtke_naziv]
barButtonItem.Name = [dabase]
Tvrtka.Items.Add(barButtonItem)
End Function
问题是,在第二个功能中,我应该为每个"实现一个"循环,因为第一个函数将返回多行。
我如何实现这一目标?
答案 0 :(得分:0)
更改您的函数以将数据添加到此类的列表中:
Public Class ReturnList
Public Property tvrtke_naziv As string
Public Property dabase As string
End class
这里是将结果输入列表的函数:
Public Sub vratiTvrtke(ByVal hwid As String)
Dim hardware As String = hwid
Dim result = New List(Of ReturnList)
Try
ManageConnection(False, konekcija) 'Open connection'
Dim strQuery As String = "SELECT tvrtke_naziv, dabase FROM info.tvrtke;"
Dim SqlCmd As New MySqlCommand(strQuery, dbCon)
Dim reader As MySqlDataReader = SqlCmd.ExecuteReader()
While reader.Read()
Dim TempResult As New ReturnList
TempResult.tvrtke_naziv = reader(0)
TempResult.dabase = reader(1)
result.Add (TempResult)
End While
reader.Close()
Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
ManageConnection(True, konekcija) 'Close connection
End Try
pripremiSucelje(result)
End Function
然后只需添加For Each
循环:
Public Function pripremiSucelje(ByVal MyList As List(Of ReturnList)
For Each item As ReturnList in MyList
Dim barmanager1 As New BarManager
Dim barButtonItem = New BarButtonItem()
barButtonItem.Content = item.tvrtke_naziv
barButtonItem.Name = item.dabase
Tvrtka.Items.Add(barButtonItem)
Next
End Function