如何使用vb.net中的GetSchema获取数据库中表的名称

时间:2015-03-06 01:06:13

标签: mysql vb.net

我有以下代码。

Dim conn As New MySqlConnection
    Dim command As New MySqlCommand
    Dim dt As New DataTable
    Dim dt1 As New DataTable
    Dim dt2 As New DataTable
    conn.ConnectionString = "server=localhost;userid=root;password=NewPass;database=converter"
    Try
        conn.Open()
        dt = conn.GetSchema("TABLES")

        For i As Integer = 0 To dt.Columns.Count - 1
            MsgBox(dt.Columns(i).ToString)
        Next

        ComboBox1.ValueMember = "table_name"
        ComboBox1.DisplayMember = "table_name"
        ComboBox1.DataSource = dt

        command.Dispose()
        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try

组合框内的值是表格的名称(这里没有问题)。我想要做的是在将特定列放入组合框之前删除它。问题来了,我尝试在dt(datatable)中显示列,但它没有显示表的名称。它显示了这个结果。

  • TABLE_CATALOG
  • TABLE_SCHEMA
  • TABLE_NAME
  • AND SO ON ......

如何在我的dt中获取表格的名称?感谢。

1 个答案:

答案 0 :(得分:1)

您的查询应该是......

select table_name from information_schema.tables

运行此查询,即可获得所需内容。同时检查下面的代码以填充数据表...

  Dim dt As New DataTable
  Dim adptr As MySqlDataAdapter
   Using conn As New MySqlConnection(YourConnectionString)
    Using cmd As New MySqlCommand("Select table_name from information_schema.tables", conn)
       Try
            conn.Open()
            adptr = New MySqlDataAdapter(cmd)
            adptr.Fill(dt)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
   End Using
 End Using

 If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then         
    ComboBox1.ValueMember = "table_name"
    ComboBox1.DisplayMember = "table_name"
    ComboBox1.DataSource = dt
 End If