vb.net 在VB.NET中将mysql数据添加到列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15583370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:56:35  来源:igfitidea点击:

Add mysql data to listbox in VB.NET

mysqlvb.netlistbox

提问by Tim Rowley

I need to make VB take information from my MySQL Database and place it in a list box. So please could you help me out. I can't seem to understand how to insert it into a list box.

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

回答by Rajaprabhu Aravindasamy

I hope this code will help you to get an idea about what you are looking for.

我希望此代码将帮助您了解您正在寻找的内容。

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