2012-10-02 69 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) –