gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:21:50 24 4
gpt4 key购买 nike

我有以下代码。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com