将值设置为数组,然后为每个设置

时间:2017-05-23 12:24:25

标签: vb.net

如何在数组中设置以下内容:

Public Function opcijeMp(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 * FROM info.opcije_mp as mp inner join instalacije as i where mp.idopcije_mp = 
        i.opcijeMP and i.instalacije_hwid  = '" + Globals.cpuid + "';"
        Dim SqlCmd As New MySqlCommand(strQuery, dbCon)
        Dim reader As MySqlDataReader = SqlCmd.ExecuteReader()
        While reader.Read()
            Globals.prodaja = reader.GetString("Prodaja")
            Globals.Kalkulacije = reader.GetString("Kalkulacije")
            Globals.Zaduznice = reader.GetString("Zaduznice")
            Globals.Predisponacije = reader.GetString("Predisponacije")
            Globals.Robno = reader.GetString("Robno")
            Globals.KUF = reader.GetString("KUF")
            Globals.KIF = reader.GetString("KIF")
            Globals.Narudzbenice = reader.GetString("Narudzbenice")
            Globals.Nalozi = reader.GetString("Nalozi")
            Globals.akcijskeCijene = reader.GetString("Akcijske_cijene")
            Globals.servisnaRoba = reader.GetString("Servisna_roba")
            Globals.Ostalo1 = reader.GetString("Ostalo1")
            Globals.Ostalo2 = reader.GetString("Ostalo2")
            Globals.Ostalo3 = reader.GetString("Ostalo3")
        End While
        reader.Close()
        'Vraća podatke u Listi stringova
        'Return result
    Catch ex As MySqlException
        Console.WriteLine("Error: " & ex.ToString())
        Return Nothing
    Finally
        ManageConnection(True, konekcija)
    End Try
End Function

所以我可以在每个循环的下一个函数中使用它:

 Dim s As String = Globals.prodaja
    Dim parts As String() = s.Split(New Char() {","c})
    Dim icona As String = parts(1)
    Dim barmanager1 As New BarManager
    Dim TileBarItem = New TileBarItem()
    TileBarItem.Content = parts(3)
    TileBarItem.Name = "ffss"
    TileBarItem.Width = 150
    Icon = New BitmapImage(New Uri("pack://application:,,,/DevExpress.Images.v16.1;component/Images/" + icona + ""))
    TileBarItem.TileGlyph = Icon
    TileBarItem.Background = New SolidColorBrush(DirectCast(ColorConverter.ConvertFromString(parts(2)), Color))
    MessageBox.Show(parts(2))
    maloprodaja.Items.Add(TileBarItem)

现在我必须为我存储在全局变量类中的每个变量运行函数,我想将第一个函数的所有结果添加到Globals中的一个数组中,然后为每个循环运行第二个函数来填充我的tilebar

1 个答案:

答案 0 :(得分:0)

使用像Globals这样的外部变量传递数据是极差的做法。问题中的第一个函数应该return数据,或者它应该返回MySqlDataReader。这将简化您以后尝试做的事情,并有效地解决这个问题。

我也是这样看到的:

Dim strQuery As String = "SELECT * FROM info.opcije_mp as mp inner join instalacije as i where mp.idopcije_mp = 
    i.opcijeMP and i.instalacije_hwid  = '" + Globals.cpuid + "';"

我想强调这一部分:

" ... and i.instalacije_hwid  = '" + Globals.cpuid + "';"

很难理解这有多糟糕。我无法想出一个更好的方法来破解程序。谷歌用于参数化查询并学习如何使用它们而不是字符串连接,将你的cpuid放入sql语句。