Getting list of server instances installed reliably

谁说胖子不能爱 提交于 2020-01-14 15:26:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!