【问题标题】:How to display the names of my SQL Server tables in a listbox using vb.net? [duplicate]如何使用 vb.net 在列表框中显示我的 SQL Server 表的名称? [复制]
【发布时间】:2016-03-22 15:52:28
【问题描述】:

我想使用 vb.net 在列表框中显示我在 SQL Server 数据库中的表的名称。

我试过了,但它不起作用:

 connetionString = "Data Source=ABDELOUAHED;Initial Catalog=table_creances;integrated security=true"
 connection = New SqlConnection(connetionString)

 sql = "select * from table_creances"

 Try
        connection.Open()
        adapter = New SqlDataAdapter(sql, connection)
        ds.Clear()
        adapter.Fill(ds)
        connection.Close()
        ListBox1.DataSource = ds.Tables(0)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

table_creances 是我的数据库的名称。

非常感谢任何帮助。

【问题讨论】:

  • SELECT name FROM sys.tables

标签: sql-server database vb.net


【解决方案1】:

尝试使用系统视图sys.objects

试试这个代码:

Using connection As New SqlConnection(connetionString)
    connection.Open()
    adapter = New SqlDataAdapter("SELECT name FROM sys.tables", connection)
    Using ds = new DataSet()
        adapter.Fill(ds)
        with ListBox1
            .DataSource = ds.Tables(0)
            .DisplayMember = "name"
            .ValueMember = "name"
        end with
    End Using
End Using

【讨论】:

  • 或者更好:对于表格使用sys.tables
  • 我的列表框上有 system.data.grid.rows
  • 你设置了 sql = "SELECT name FROM sys.tables" 吗?
  • 非常感谢它运行良好:)
  • 但问题应该是:如何使用vb将查询设置为LsListBox的DataSource
【解决方案2】:

试试这个作为你的 sql

sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"

【讨论】:

  • 我的列表框上有 system.data.grid.rows
  • 在分配数据源之前尝试设置列表框的 DisplayMember 和 ValueMemeber ListBox1.DisplayMember = "TABLE_NAME" ListBox1.ValueMember = "TABLE_NAME ListBox1.DataSource = ds.Tables(0)
  • 同样的问题 system.data.grid.row
猜你喜欢
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 2010-09-15
相关资源
最近更新 更多