获取本机SqlServer名称

using System.Data.Sql; 

//检索包含有关可用SQL Server实例的信息的表,必须先使用共享/静态Instance属性来检索枚举器
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;  
//调用 GetDataSources 方法,返回包含可用服务器的相关信息的DataTable
System.Data.DataTable table = instance.GetDataSources(); 
//取出服务器名称
string servername=table.Rows[0]["ServerName"].ToString();
      
 

返回的table信息:

Column Description
ServerName Name of the server.
InstanceName Name of the server instance. Blank if the server is running as the default instance.
IsClustered Indicates whether the server is part of a cluster.
Version Version of the server. For example:
  • 9.00.x (SQL Server 2005)
  • 10.0.xx (SQL Server 2008)
  • 10.50.x (SQL Server 2008 R2)
  • 11.0.xx (SQL Server 2012)|

参考信息来源:(https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/enumerating-instances-of-sql-server?redirectedfrom=MSDN)

原文地址:https://www.cnblogs.com/hztd/p/12713110.html