How to Find All SQL Server Instance Running in Local Network c#

原创 2011年01月12日 10:41:00

Create new class its name to GetInstance and write this below code on GetInstance class.

using System.Data.Sql;
using System.Collections;
using System.Data;

namespace SqlServer
{
    public class GetInstance
    {
        public static ArrayList GetInstanceName()
        {
            try
            {
                SqlServerList SqlSL = new SqlServerList();
                SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
                DataTable table = instance.GetDataSources();
                ArrayList list = new ArrayList();
                foreach (DataRow row in table.Rows)
                {
                    SqlSL = new SqlServerList();
                    SqlSL.ServerName = row[0].ToString();
                    SqlSL.InstanceName = row[1].ToString();
                    SqlSL.IsClustered = row[2].ToString();
                    SqlSL.Version = row[3].ToString();
                    list.Add(SqlSL);
                }
                return list;
            }
            catch
            {
                return null;
            }
        }
    }
}

Create new class its name to SqlServerList and write this below code on SqlServerList class.

using System;

namespace SqlServer
{
    [Serializable]
    class SqlServerList : IComparable, ICloneable
    {
        public SqlServerList()
        {
            ServerName = string.Empty;
            InstanceName = string.Empty;
            IsClustered = string .Empty ;
            Version = string.Empty;
        }

        #region ICloneable Members

        public object Clone()
        {
            try
            {
                if (this == null)
                {
                    return null;
                }
                SqlServerList SqlSL = new SqlServerList { ServerName = ServerName, InstanceName = InstanceName, IsClustered = IsClustered, Version = Version };
                return SqlSL;
            }
            catch
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            try
            {
                if (!(obj is SqlServerList))
                {
                    throw new Exception("obj is not an instance of SqlServerList");
                }
                if (this == null)
                {
                    return -1;
                }
                return ServerName.CompareTo((obj as SqlServerList).ServerName);
            }
            catch
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        public string ServerName { get; set; }
        public string InstanceName { get; set; }
        public string IsClustered { get; set; }
        public string Version { get; set; }
    }
}

use this class :

 

System.Collections.ArrayList AllInstanceSqlserver = SqlServer.GetInstance.GetInstanceName();

相关文章推荐

How to backup and restore database in SQL Server

How to backup and restore database in SQL Server

How to delete duplicate rows without children in sql server

/*************written by Jiangong SUN**************/ I will introduce you how to delete unecess...

Tuning options for SQL Server when running in high performance workloads

原文链接:https://support.microsoft.com/en-us/kb/920093Contents This article describes various trace flag...

SQL Server同时操作(all-in-once)特性

SQL Server同时操作(all-in-once)特性

What is Index and how it works in SQL Server?

What is Index and how it works in SQL Server?

How to setup linked servers for SQL Server and Oracle 64 bit client

How to setup linked servers for SQL Server and Oracle 64 bit client ProblemWe have two SQL 2005 64-b...

How to determine SQL Server database transaction log usage

原文链接:https://www.mssqltips.com/sqlservertip/1225/how-to-determine-sql-server-database-transaction-lo...

[How To]如何给SQL Server配置证书

默认情况下从SQL Server 2005开始SQL Server会使用自动生成的证书来加密与客户的连接和数据包传输。你也可以给SQL Server配置你自己的证书来实现更高的安全性。  证书从哪里获...
  • apgcdsd
  • apgcdsd
  • 2011年04月21日 10:54
  • 445

How to Configure DG4MSQL (Oracle Database Gateway for MS SQL Server) 64bit Unix OS (Linux, Solaris,

How to Configure DG4MSQL (Oracle Database Gateway for MS SQL Server) 64bit Unix OS (Linux, Solaris, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to Find All SQL Server Instance Running in Local Network c#
举报原因:
原因补充:

(最多只允许输入30个字)