Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Get list of SQL databases on selected server

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
0
d10u4v
Asked:
d10u4v
1 Solution
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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

0
 
d10u4vAuthor 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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now