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 Broswer is runing and 2 - the UDP port 1434 is open.
Anybody knows why? Thanks.
Something People need to know about the methods of GetDataSources and SqlDataSourceEnumerator. If the Instance Name is the default - InstanceName will be blank! [Why .. I do not know, why can't I specify verbose, don't know that either but the guy at MS that wrote it ... arrrgh]
ServerName: Name of the server.
InstanceName: Name of the server instance. Blank if the server is running as the default instance.
IsClustered Indicates whether the server is part of a cluster.
Version Version of the server (8.00.x for SQL Server 2000, and 9.00.x for SQL Server 2005).
FROM HERE: https://msdn.microsoft.com/en-us/library/system.data.sql.sqldatasourceenumerator.getdatasources(v=vs.110).aspx
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.
You are skipping over servers that are not named instances. Modify your code:
Please Note:
SqlDataSourceEnumerator.Instance.GetDataSources()
has drawbacks:Several sources say you have to make 2 calls to
SqlDataSourceEnumerator.Instance.GetDataSources()
...Refs: