Get list of SQL databases on selected server

Posted on 2009-02-14
Last Modified: 2013-12-25

I'm in the process of finalising my project and i'm creating a sonnection setup form.  Is there any way of, once a user has entered a server address for the SQL server, populating a combobox with a list of database found on that server?

My form has:


Question by:d10u4v
    LVL 84

    Accepted Solution

    See the DMO example here:

    This is for ASP, but you could fill your combo with it as well by changing it:

        strSQLServer = "server_name" 
        strLogin = "user_name" 
        strPassword = "password" 
    ' ######################### 
        Set oServer = CreateObject("SQLDMO.SQLServer") 
        oServer.Name = strSQLServer 
        ' Check the server is alive 
        If oServer.Status <> 1 Then 
            Response.Write "Server " & strSQLServer & " is not running. Exiting." 
        End If 
        ' Connect to the SQL Server instance 
        oServer.LoginSecure = False 
        oServer.Connect strSQLServer, strLogin, strPassword 
        Me.YourCombo.RowSourcetype = "Value List"
        ' We're connected to the server let's iterate through the Databases Collection 
        For Each oDB in oServer.Databases 
          Me.YourCombo.AddItem oDB.Name
        ' Disconnect from SQL Server 
        set oServer = nothing 

    Open in new window


    Author Comment

    That worked great!  I'll get the data for the server and login details from the user, so as to not store them in the code.  This should make setting up users computers a lot easier.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now