问题
I am using following code to get a list of server instances installed.
Dim sqldatasourceenumerator1 As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
datatable1 = sqldatasourceenumerator1.GetDataSources()
Sometimes this code works fine but most of the times it gets lost and system becomes unresponsive.
Could anyone advise me some alternative code that is reliable in all conditions? Thanks
回答1:
You could check the registry, for example you can try reading this registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL
32 bit instances on a 64 bit OS should be listed under:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL
Here is a c# code snippet to get this info for 64 bit instances on 64 bit Windows:
RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL");
foreach (string s in key.GetValueNames())
{
...
}
key.Close();
baseKey.Close();
来源:https://stackoverflow.com/questions/17337724/getting-list-of-server-instances-installed-reliably