在VB.NET中将mysql数据添加到列表框[关闭]

问题描述 投票:-3回答:1

我需要让VB从MySQL数据库中获取信息并将其放在列表框中。那么请你帮帮我。我似乎无法理解如何将其插入列表框。

mysql vb.net listbox
1个回答
1
投票

我希望这段代码可以帮助您了解您正在寻找的内容。

Private sub FillListBox

    Dim stringConn As String
    Dim stringCmd As String
    Dim myConn As MySqlConnection
    Dim myCmd As MySqlCommand

    'Frame your query here.
    stringCmd = "SELECT yourData FROM yourTable"

    'Frame your connection string here.
    stringConn = "SERVER=localhost;DATABASE=DBName;UID=root;PASSWORD=;"

    'Get your connection here.
    myConn = New MySqlConnection(stringConn)

    'Get a command by using your connection and query.
    myCmd = New MySqlCommand(stringCmd, myConn)

    'Open the connection.
    myConn.Open()

    'create a reader to store the datum which will be returned from the DB
    Dim myReader As MySqlDataReader

    'Execute your query using .ExecuteReader()
    myReader = myCmd.ExecuteReader()

    'Reset your List box here.
    ListBox1.items.clear()

    While (myReader.Read())
            'Add the items from db one by one into the list box.
        ListBox1.items.add(myReader.GetString(1))
    End While

    'Close the reader and the connection.
    myReader.Close()
    myConn.Close()

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.