2013-03-23 61 views
-3

我需要讓VB從我的MySQL數據庫中獲取信息並將其放置在列表框中。所以請你能幫我一把。我似乎無法理解如何將其插入列表框中。在VB.NET中將mysql數據添加到列表框中

+0

在這一點你有問題嗎? – luchosrock 2013-03-23 04:34:16

+0

我不知道如何引入數據! – 2013-03-23 04:51:27

+0

發佈我們的代碼,你已經嘗試過。 – 2013-03-23 04:54:21

回答

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