Retrieving SQL Server 2005 Database Info using SMO: Scripting Tables, Views, Stored Procedures - Populating the dropdown lists for “databases” and “tables” using SMO
(Page 2 of 6 )
To prepare this utility, we need to load the database list into a dropdown list. Once you select some databases from the database dropdown list, you need to populate another dropdown list with the entire tables list (related to the selected database). This section deals with the same.
Let us consider the following code first:
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim svr As Server = New Server(".\sql2k5")
Me.ComboBox1.DataSource = util.getDatabaseList(svr)
Me.ComboBox1.DisplayMember = "DatabaseName"
ComboBox1_SelectedIndexChanged(Nothing, Nothing)
End Sub
The above code populates a dropdown list with all the database names. When the user selects any database from the list, the following code populates all the table names:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
Dim svr As Server = New Server(".\sql2k5")
Me.ComboBox2.DataSource = util.getTableList(svr, svr.Databases(Me.ComboBox1.Text))
Me.ComboBox2.DisplayMember = "TableName"
Me.ComboBox2.ValueMember = "Schema"
Catch ex As Exception
Me.ComboBox2.DataSource = Nothing
Me.ComboBox2.Items.Clear()
End Try
End Sub
I am using two of the routines explained in the previous section in both of the above code fragments.
Next: How to retrieve all column info from an SQL Server instance using SMO >>
More MS SQL Server Articles
More By Jagadish Chaterjee