Create new class its name to GetInstance and write this below code on GetInstance class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 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 :
1 | System.Collections.ArrayList AllInstanceSqlserver = SqlServer.GetInstance.GetInstanceName(); |



Comments