We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Get list of SQL databases on selected server

Medium Priority
195 Views
Last Modified: 2013-12-25
Hi,

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:

Server:
Database:
Username:
Password:

Regards,
Tom
Comment
Watch Question

Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
See the DMO example here:

http://databases.aspfaq.com/schema-tutorials/schema-how-do-i-list-the-databases-on-my-server.html

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
    Next 
 
    ' Disconnect from SQL Server 
    oServer.Disconnect 
    set oServer = nothing 

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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.

Thanks
Tom
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.