Get the names of the databases on a SQL Server

Posts   
 
    
chrishilton avatar
Posts: 49
Joined: 26-Jun-2007
# Posted on: 04-Sep-2007 13:46:14   

I'm using LLBLGen Pro 2.0.0.0 Final with adaptor and .NET 2.0/c# connecting to a SQL Server 2005 dB.

Within my application I would like a user to have the ability to select the database that they will connect to - best example is a customer who may have a development and production database.

To accomplish this I'd like to have a screen where the user enters the connection properties (ie. server name, user and password) and from this I would like them to choose the database to connect to from a list of the databases on the server.

So my question is can I retrieve a list of the databases on the server using LLBLGen Pro?

To do this via an adhoc query I could simply execute select [name] from sys.databases. I know that the designer filters out the master database so I can't create a project and entity for the system view and neither can I create my own view on the system view in my existing project as I need a list of databases before knowing which database relates to my project.

Any help or advice would be greatly appreciated.

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 04-Sep-2007 15:16:14   
    Private Sub FillServerList()
        Dim servers As ArrayList

        Try
            Me.Cursor = Cursors.WaitCursor
            cmbServerName.Items.Clear()
            servers = TodayIT.Avalanche.DbUpdater.SqlSmoFunctions.GetSqlServers()

            'get servers and refresh the catalog combo for the first selected server
            If servers IsNot Nothing AndAlso servers.Count > 0 Then
                For Each servername As String In servers
                    cmbServerName.Items.Add(servername)
                Next
            End If

        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox("Detection failed, SQLSMO might not be installed. Details: " & ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub FillCatalogList()
        Try
            Me.Cursor = Cursors.WaitCursor
            Using sdf As New TodayIT.Avalanche.DbUpdater.SqlSmoFunctions(cmbServerName.Text, txtUserId.Text, txtPassword.Text) 'TodayIT.Avalanche.Shared.SqlDmoFunctions(cmbServerName.Text)
                cmbCatalog.Items.Clear()
                For Each database As String In sdf.GetDatabases()
                    cmbCatalog.Items.Add(database)
                Next
            End Using
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox("Could not fill the catalog list, please check your rights to this server. Details: " & ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub


    Public Shared Function GetSqlServers() As ArrayList
        Dim result As New ArrayList
        Dim srv As System.Data.DataRow
        Try
            Dim servers As System.Data.DataTable = SmoApplication.EnumAvailableSqlServers(False)
            For Each srv In servers.Rows
                result.Add(srv("Name").ToString())
            Next
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
        Return result
    End Function

    Public Function GetDatabases() As ArrayList
        Dim result As New ArrayList
        Dim db As Database
        Try
            For Each db In _sqlServer.Databases
                result.Add(db.Name)
            Next
        Catch ex As Exception
            _errMessage = ex.Message
        Finally
            db = Nothing
        End Try

        Return result
    End Function

chrishilton avatar
Posts: 49
Joined: 26-Jun-2007
# Posted on: 04-Sep-2007 16:04:17   

Thanks very much for the steer DvK

Found this related article for anyone who's interested in SQLSMO:

http://msdn.microsoft.com/msdnmag/issues/07/06/DataPoints/default.aspx