Loading database into combobox as well server name that are available...
Import reference:
//This is available in sql sever SDK Folder
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Smo
//Use namespace to import.
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
Declare Object of Server Class
Code in load event of form:
//datatable object
DataTable dt = SmoApplication.EnumAvailableSqlServers(true);
if (dt.Rows.Count > 0)
{
// Loop through each server in the DataTable
foreach (DataRow dr in dt.Rows)
{
//cmbsrv_name is name of combo box in //which all server name will be loaded..
cmbsrv_name.Items.Add(dr["Name"]);
}
}
To load all database code:
This code will be in button click event to connect with server.
//cmbsrv_name is name of combo in which all server name loaded.
if (cmbsrv_name.SelectedItem != null && cmbsrv_name.SelectedItem.ToString() != "")
{
ServerConnection srvcon = new ServerConnection(cmbsrv_name.SelectedItem.ToString());
srvcon.LoginSecure = false;
//txtuserid is id of server and txtpassword password of server to connect
srvcon.Login = txtuserid.Text;
srvcon.Password = txtPassword.Text;
srv = new Server(srvcon);
foreach (Database db in srv.Databases)
{
//cmbdatabase is name of combobox in which all database related to that server will be loaded
cmbDatabase.Items.Add(db.Name);
}
}
Thanks you