Using the SQLDMO library you can use the following code to populate a dropdown list
//get all available SQL Server
SQLDMO.NameList sqlServers
tr
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
sqlServers = sqlApp.ListAvailableSQLServers();
catch (Exception err
MessageBox.Show(err.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
return
for(int i=0; i < sqlServers.Count; i++)
{
object srv = sqlServers.Item(i + 1);
if(srv != null)
{
this.cboSqlServers.Items.Add(srv);
}
}
if(this.cboSqlServers.Items.Count > 0)
this.cboSqlServers.SelectedIndex = 0;
else
this.cboSqlServers.Text = "<No available SQL Servers>";
And then to get a list of databases for the selected server
//get all available databases from an SQL Server
tr
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
srv.Connect(this.cboSqlServers.SelectedItem.ToString(),this.txtUserName.Text,this.txtPassword.Text);
foreach(SQLDMO.Database db in srv.Databases)
{
if(db.Name!=null)
this.cboDatabases.Items.Add(db.Name);
if(this.cboDatabases.Items.Count > 0)
this.cboDatabases.SelectedIndex = 0;
catch (Exception err
MessageBox.Show(err.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error)