我有以下代码。

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(数据表)中的列,但不显示表的名称。它显示了这个结果。


TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
等等...


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

最佳答案

您的查询应该是...

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

关于mysql - 如何在vb.net中使用GetSchema获取数据库中表的名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28890732/

10-09 00:53