简体   繁体   English

在本地网络上查找所有SQL Server实例

[英]Find all SQL Server instances on local nework

I am trying to get all available SQL Server to name with their instance name, I am able to find all available SQL Server names on a local LAN but I am unable to find their instance names on some SQL Server 我正在尝试使用其实例名称来命名所有可用的SQL Server,我能够在本地LAN上找到所有可用的SQL Server名称,但在某些SQL Server上却找不到它们的实例名称

I need SQL Server instance name because some servers have default instance and some servers have specific instance names like pcname\\sqlexpress 我需要SQL Server实例名称,因为某些服务器具有默认实例,而某些服务器具有特定的实例名称,例如pcname\\sqlexpress

I am this code to get all SQL Server instances .. 我是这段代码来获取所有SQL Server实例..

 DataTable dt = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

and I am getting these results .. 而我得到这些结果..

在此处输入图片说明

There are lots of reasons why this call would fail in full or partially. 有很多原因导致此调用全部或部分失败。

From MSDN : 从MSDN

The list can vary depending on factors such as timeouts and network traffic. 该列表可能会因超时和网络流量等因素而有所不同。 This can cause the list to be different on two consecutive calls. 这可能导致列表在两个连续的呼叫中不同。 Only servers on the same network will be listed. 仅列出同一网络上的服务器。 Broadcast packets typically won't traverse routers, which is why you may not see a server listed, but it will be stable across calls. 广播数据包通常不会穿过路由器,这就是为什么您可能看不到列出的服务器的原因,但是它在所有呼叫中都是稳定的。 Listed servers may or may not have additional information such as IsClustered and version. 列出的服务器可能具有也可能没有其他信息,例如IsClustered和版本。 This is dependent on how the list was obtained. 这取决于列表的获取方式。 Servers listed through the SQL Server browser service will have more details than those found through the Windows infrastructure, which will list only the name. 通过SQL Server浏览器服务列出的服务器将比通过Windows基础结构找到的服务器具有更多详细信息,Windows基础结构仅列出名称。

Server enumeration is only available when running in full-trust. 仅在完全信任下运行时,服务器枚举才可用。 Assemblies running in a partially-trusted environment will not be able to use it, even if they have the SqlClientPermission Code Access Security (CAS) permission. 即使它们具有SqlClientPermission代码访问安全性(CAS)权限,在部分受信任的环境中运行的程序集也将无法使用它。 SQL Server provides information for the SqlDataSourceEnumerator through the use of an external Windows service named SQL Browser. SQL Server通过使用名为SQL Browser的外部Windows服务为SqlDataSourceEnumerator提供信息。 This service is enabled by default, but administrators may turn it off or disable it, making the server instance invisible to this class. 默认情况下启用此服务,但是管理员可以关闭或禁用此服务,从而使服务器实例对该类不可见。

As @Orlando notes this may give you some solutions: 正如@Orlando指出的那样,这可能会为您提供一些解决方案:

Find all instances of sql server programmatically 以编程方式查找SQL Server的所有实例

run a stored procedure that executes this in your C# side 运行在C#端执行此操作的存储过程

select @@servername + '\' + @@servicename

and return it in a datatable, then use them. 并将其返回到数据表中,然后使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM