简体   繁体   English

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

[英]How to get the name of tables in a database using GetSchema in vb.net

I have the following code. 我有以下代码。

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

The values inside the combobox are the names of the tables (there's no problem here). 组合框内的值是表的名称(这里没有问题)。 What I want to do is to remove a specific column BEFORE putting it to the combobox. 我想要做的是在将特定列放入组合框之前将其删除。 Here comes the problem, I tried displaying the columns inside the dt (datatable) but it does not show the names of the tables. 问题来了,我尝试显示dt(数据表)中的列,但不显示表的名称。 It shows this results. 它显示了这个结果。

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

How do I get the names of my tables in my dt? 如何在dt中获取表的名称? Thanks. 谢谢。

Your query should be... 您的查询应该是...

select table_name from information_schema.tables

Run this query and you get what you need. 运行此查询,您会得到所需的信息。 Also check code below for filling a datatable... 还要检查下面的代码以填充数据表...

  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM