Create a console application and give the project name as ConServerName
Step 2
Create a ServerName static class in console application,it is look like this
/// <summary> /// List of Sever Names /// </summary> public static class ServerName { #region Property /// <summary> /// Get the server names from local and server machine /// </summary> public static System.Data.DataTable ServerNamesList { get { System.Data.Sql.SqlDataSourceEnumerator SqlDSE = null; SqlDSE = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable ServerTable = SqlDSE.GetDataSources(); return ServerTable; } } #endregion }
Step 3
Print the Server name in a console,it is look like this
class Program { static void Main(string[] args) { try { DataTable Dt = ServerName.ServerNamesList; if (Dt != null) { if (Dt.Rows.Count > 0) { System.Console.WriteLine("Server Name\t\t\tInstance Name\t\t\tVersion"); for (int i = 0; i < Dt.Rows.Count; i++) { System.Console.WriteLine(Dt.Rows[i][0].ToString() + "\t\t\t\t" + Dt.Rows[i][1].ToString() + "\t\t\t" + Dt.Rows[i][3].ToString()); } } } } catch (Exception ex) { System.Console.WriteLine(ex.Message); } } }
Full Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.Sql; namespace ConServerName { /// <summary> /// List of Sever Names /// </summary> public static class ServerName { #region Property /// <summary> /// Get the server names from local and server machine /// </summary> public static System.Data.DataTable ServerNamesList { get { System.Data.Sql.SqlDataSourceEnumerator SqlDSE = null; SqlDSE = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable ServerTable = SqlDSE.GetDataSources(); return ServerTable; } } #endregion } class Program { static void Main(string[] args) { try { DataTable Dt = ServerName.ServerNamesList; if (Dt != null) { if (Dt.Rows.Count > 0) { System.Console.WriteLine("Server Name\t\t\tInstance Name\t\t\tVersion"); for (int i = 0; i < Dt.Rows.Count; i++) { System.Console.WriteLine(Dt.Rows[i][0].ToString() + "\t\t\t\t" + Dt.Rows[i][1].ToString() + "\t\t\t" + Dt.Rows[i][3].ToString()); } } } } catch (Exception ex) { System.Console.WriteLine(ex.Message); } } } }
Output
Download
Download Source Code
No comments:
Post a Comment