2012-10-02 70 views
-1

我是編程新手,我正在研究一個基本的VB.NET應用程序,允許用戶從MySQL中選擇,插入,更新和刪除各種數據表。vb.net mysql combobox show tables

我遇到的麻煩是,我需要用一個特定數據庫中的所有表名填充組合框,以便用戶可以選擇使用哪個數據庫表。我認爲我的代碼可以工作,但是當我運行該應用時,我得到的所有內容都是空白的組合框。

有人能告訴我我的代碼有什麼問題嗎?

非常感謝!

代碼:

Private Sub TableList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles TableList.SelectedIndexChanged 

    Try 

     command = New MySqlCommand 
     dt = New DataTable 
     adapter = New MySqlDataAdapter 

     If (conn.State = ConnectionState.Closed) Then 
      setConnection() 
     End If 

     command.Connection = conn 
     command.CommandText = "SHOW TABLES" 

     adapter.SelectCommand = command 
     reader = command.ExecuteReader 

     'adapter.Fill(dt) 
     dt.Load(reader) 

     TableList.DataSource = dt 
     TableList.DisplayMember = "Tables_in_sampledata" 'What is displayed 
     TableList.ValueMember = "Tables_in_sampledata" 'The ID of the row 

    Catch ex As MySqlException 
     MessageBox.Show("Error1: " & ex.Message) 
    Finally 
     reader.Close() 
     conn.Close() 
    End Try 

End Sub 
+0

什麼錯誤的,你得到什麼? –

回答

0

相反的SHOW TABLES,使用以下查詢

SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA='YourDatabase'; 
+0

嗨,John,謝謝你的幫助。我試過了你的查詢,但它沒有做任何事 - 我仍然得到一個空白的組合框。即使當我嘗試像「SELECT * FROM'databaseName'」這樣的基本SQL查詢時,我仍然沒有收到任何東西。 – phoeberesnik

+0

@ user1713373您沒有指定數據集的數據表來設置數據源。 ex,'TableList.DataSource = dt.Tables [0]' –

+0

@ user1713373 [點擊這裏](http://msdn.microsoft.com/en-us/library/w67sdsex.aspx) –