我想使用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
是我的数据库的名称。
非常感谢任何帮助。
答案 0 :(得分:0)
尝试将此作为您的sql
sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"
答案 1 :(得分:0)
尝试使用系统视图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