Пример #1
0
        public static DataTable GetDataBaseServers()
        {
            DataTable table = new DataTable();

            table.Columns.Add(new DataColumn("Name"));
            foreach (string str in WindowsService.WindowsServices)
            {
                if (str.ToUpper() == "MSSQLSERVER")
                {
                    table.Rows.Add(new object[] { Dns.GetHostName().ToUpper() });
                }
                else if (str.Contains("MSSQL$"))
                {
                    table.Rows.Add(new object[] { string.Format(@"{0}\{1}", Dns.GetHostName().ToUpper(), str.Replace("MSSQL$", "")) });
                }
            }
            try
            {
                if (table.Rows.Count == 0)
                {
                    Application application = new ApplicationClass();
                    NameList    list        = application.ListAvailableSQLServers();
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list.Item(i + 1).Contains(Dns.GetHostName()))
                        {
                            table.Rows.Add(new object[] { list.Item(i + 1) });
                        }
                    }
                    if (table.Rows.Count == 0)
                    {
                        foreach (DataRow row in SmoApplication.EnumAvailableSqlServers(true).Rows)
                        {
                            table.Rows.Add(new object[] { row["name"] });
                        }
                    }
                }
                if (table.Rows.Count != 0)
                {
                    return(table);
                }
                RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL");
                foreach (string str2 in key.GetValueNames())
                {
                    table.Rows.Add(new object[] { (str2.ToUpper() == "MSSQLSERVER") ? Dns.GetHostName().ToUpper() : str2.ToUpper() });
                }
            }
            catch (Exception exception)
            {
                Log.WriteLog("获取本机安装数据库服务器:GetDataBaseServers()", exception.Message);
            }
            return(table);
        }
Пример #2
0
        public ArrayList GetServerList()
        {
            ArrayList arrayLists = new ArrayList();

            SQLDMO.Application applicationClass = new Application();
            try
            {
                try
                {
                    NameList nameList = applicationClass.ListAvailableSQLServers();
                    for (int i = 1; i <= nameList.Count; i++)
                    {
                        arrayLists.Add(nameList.Item(i));
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception(string.Concat("取数据库服务器列表出错:", exception.Message));
                }
            }
            finally
            {
                applicationClass.Quit();
            }
            return(arrayLists);
        }