I use the following code to list all the remote and local SQL Server instances:
public static void LocateSqlInstances()
{
using( DataTable sqlSources = SqlDataSourceEnumerator.Instance.GetDataSources())
{
foreach(DataRow source in sqlSources.Rows )
{
string instanceName = source["InstanceName"].ToString();
if (!string.IsNullOrEmpty(instanceName))
{
Console.WriteLine(" Server Name:{0}", source["ServerName"]);
Console.WriteLine(" Instance Name:{0}", source["InstanceName"]);
Console.WriteLine(" Version:{0}", source["Version"]);
Console.WriteLine();
}
}
Console.ReadKey();
}
}
running the code on my local machine. The code can find and list a SQL server express instance (version 9.0.5000) installed but failed to list the other SQL server instance (version 10.0.1600).
I’ve done a lot of research on the Internet and made sure that (1) the Sql Browser is running and (2) the UDP port 1434 is open.
Anybody knows why? Thanks.
Thanks a lot to Mitch for the great answer he puts together. However, what I’ve done eventually is like the following:
I have two separate methods to get local and remote server instance respectively. The local instances are retrieved from the registry. You need to search both WOW64 and WOW3264 hives to get both SQL server 2008 (64bit) and SQL server Express (32 bit)
here is the code I use:
At the end, I just merge the remote instance list and local instance list to produce the final result.