1 Previous Next 

SQL Server: List Databases and Tables with SMO


Visual studio 2005 introduced a new SMO class.  You can use it to list the database and tables in an sql server.  This example list the database and tables on the local sql server. 

Add a reference to Microsoft.SqlServer.ConnectionInfo and  Microsoft.SqlServer.Smo

Imports Microsoft.SqlServer.Management.Smo

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim srv As New Server(".")
        Dim db As Database
        For Each db In srv.Databases
            Trace.WriteLine(db.Name)
            Trace.Indent()
            For Each tbl As Table In db.Tables
                Trace.WriteLine(tbl.Name)
            Next
            Trace.Unindent()
        Next
    End Sub
End Class




1 Previous Next