Link to home
Start Free TrialLog in
Avatar of sachintha81
sachintha81Flag for United States of America

asked on

How to identify the SQL instances installed on a network, using C#?

I need to search for all the SQL Server / SQL Server Express instances installed in a newtwork and display them in a combo box in a C# Application Program.
I tried the following two codes:

But both the methods doesn't give me the results I'm hoping for.
My primary objective is to distinguish between SQL Server 2005 and SQL Server Express, because for another program to run I need my user to be able to select one among those two. But when I run this code it give the following errors:

1) I have above both installed on my computer, but it gives only the server name of SQL Server 2005.

2)On the network, this code completely ignores some of the machines, and it DOES give me both the SQL Server 2005 and Express instances on some other machines on my network. If I run the code from another machine, it still doesn't show that I ahve installed both on my computer.

Experts, please help on this.
//Using smo. 
//Here, localOnly is a bool variable. If true, this method search only the instances on this machine and if set to false search instances on all machines on the network
foreach (DataRow serverRow in SmoApplication.EnumAvailableSqlServers(localOnly).Rows)
{
                cmbServerList.Items.Add(serverRow.ItemArray[0]);
}
 
 
//Here ItemArray[0] stores each SQL server name and ItemArray[1] stores each instance
SqlDataSourceEnumerator sqlInstance = SqlDataSourceEnumerator.Instance;
foreach (DataRow dataSourceRow in sqlInstance.GetDataSources().Rows)
{
                cmbServerList.Items.Add(dataSourceRow.ItemArray[0] + "/" + dataSourceRow.ItemArray[1]);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sachintha81

ASKER

Dhaest, the last program of your link which combines all the methods pretty much gave me the functionality I desire except for one.
Still it doesn't give me the SQL server instances (not all, but some) of some of the other computers on the network. So I figured that the best way should be to do the same thing this does with the local computer - that is to access teh registry of each of the computer on the network and see if each of the servers are there in those computers. So, can that be done? Can you lookup the registry of a computer in a network?
One of the reasons that you don't find the sql-servers on other machines, can be that they are not completely correct configured (named pipes, ...) or because of security reason on those pc's...

public static string ConnectToRegistry (string servername) 
{ 
 
string regKeyToGet, keyToRead; 
 
ConnectionOptions oConn = new ConnectionOptions(); 
oConn.Username = "admin"; 
oConn.Password = "pass"; 
ManagementScope scope = new ManagementScope(@"//" + servername + @"/root/default", oConn); 
registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null); 
 
// Returns a specific value for a specified key 
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue"); 
inParams["sSubKeyName"] = regKeyToGet; 
inParams["sValueName"] = keyToRead; 
ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue", inParams, null); 
 
return outParams["sValue"].ToString(); 

Open in new window