I needed the list of SQL Server Instances to setup the database for my application to the particular server.
I did this with the help of google and stackoverflow. The code is as following:
private void ListSqlServers()
{
string myServer = Environment.MachineName;
DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
for (int i = 0; i < servers.Rows.Count; i++)
{
// Remove the following 'if' condition
// to list the servers of local machine
// along with network servers
// used to get the servers in the local machine
if (myServer == servers.Rows[i]["ServerName"].ToString())
{
if ((servers.Rows[i]["InstanceName"] as string) != null)
comboBox1.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
else
comboBox1.Items.Add(servers.Rows[i]["ServerName"]);
}
}
}
I did this with the help of google and stackoverflow. The code is as following:
private void ListSqlServers()
{
string myServer = Environment.MachineName;
DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
for (int i = 0; i < servers.Rows.Count; i++)
{
// Remove the following 'if' condition
// to list the servers of local machine
// along with network servers
// used to get the servers in the local machine
if (myServer == servers.Rows[i]["ServerName"].ToString())
{
if ((servers.Rows[i]["InstanceName"] as string) != null)
comboBox1.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
else
comboBox1.Items.Add(servers.Rows[i]["ServerName"]);
}
}
}
No comments: