How to list available instances of SQL Servers using SMO in C#?

前端 未结 4 708
小鲜肉
小鲜肉 2020-11-30 15:12

Can anybody explain me what I wrong I am doing in the following piece of code:

DataTable dt=SmoApplication.EnumAvailableSqlServer(true);
Server sr = new Serv         


        
4条回答
  •  半阙折子戏
    2020-11-30 15:50

    using Microsoft.Win32;
    
           RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
            String[] instances = (String[])rk.GetValue("InstalledInstances");
            if (instances.Length > 0)
            {
                foreach (String element in instances)
                {
                   Console.WriteLine(element);    // element is your server name                
                }
            }
    

提交回复
热议问题