【问题标题】:Displaying Table names in a listbox (VB)在列表框中显示表名 (VB)
【发布时间】:2021-11-23 13:57:13
【问题描述】:

您好,我想知道如何在列表框中显示表名的语法?

我正在使用带有 LocalDB 的 Visual Studio (vb) 并尝试使用 Windows 窗体创建一个程序,该程序可以在我的数据库中创建一个表并在列表框(或任何类似于列表框的东西)中显示表的名称

【问题讨论】:

  • 如何获取表名?
  • select name from sys.tables 也许?
  • 我还想知道是否有另一种方法可以让所有表名显示在列表框中,或者我正在考虑有一个单独的表或列来存储表名但我认为它会占用额外的空间?

标签: sql-server database vb.net windows-forms-designer localdb


【解决方案1】:

这取决于您使用的 Sql Server 版本。这应该适用于 2005 年及更高版本。

Private ConLocal As String = "Your connection string"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sql = "Select TABLE_NAME
                FROM INFORMATION_SCHEMA.TABLES
                WHERE TABLE_TYPE = 'BASE TABLE';"
    Dim dt As New DataTable
    Using cn As New SqlConnection(ConLocal),
            cmd As New SqlCommand(sql, cn)
        cn.Open()
        Using reader = cmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
    For Each row As DataRow In dt.Rows
        ListBox1.Items.Add(row(0))
    Next
End Sub

【讨论】:

  • 这个解决了我的问题,谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-02-09
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
相关资源
最近更新 更多