语言

列举 SQL Server 实例(ADO.NET)

SQL Server 允许应用程序在当前网络中查找 SQL Server 实例。 该 SqlDataSourceEnumerator 类返回 DataTable 对象中的可见服务器。 该表列出了网络上可用的服务器实例,如 “连接属性 ”对话框中的服务器列表。 结果并不总是完整的。

使用 Microsoft.Data.SqlClient 时,GetDataSources 需要配备本机网络功能的 Windows。 在启用了托管网络的 Linux、macOS 或 Windows 上,该方法将引发NotImplementedException。 在这些环境中,直接在连接字符串中指定服务器地址和端口,而不是服务器发现。

注释

与大多数 Windows 服务一样,最好以尽可能小的特权运行 SQL Browser 服务。

检索枚举器实例

若要检索有关可用 SQL Server 实例的信息,请先通过共享/静态 Instance 属性获取枚举器:

Dim instance As Microsoft.Data.Sql.SqlDataSourceEnumerator = _
   Microsoft.Data.Sql.SqlDataSourceEnumerator.Instance
Microsoft.Data.Sql.SqlDataSourceEnumerator instance =
   Microsoft.Data.Sql.SqlDataSourceEnumerator.Instance;

检索静态实例后,调用 GetDataSources 以检索 DataTable 对象中有关可用服务器的信息:

Dim dataTable As System.Data.DataTable = instance.GetDataSources()
System.Data.DataTable dataTable = instance.GetDataSources();

通过方法调用返回的表包含以下列,其中所有列都包含 string 值:

列 DESCRIPTION
ServerName 服务器的名称。
InstanceName 服务器实例的名称。 如果服务器作为默认实例运行,则为空。
IsClustered 指明服务器是否属于群集。
Version 服务器的版本。 例如:

- 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)

枚举限制

不一定会列出所有可用服务器。 此列表因超时和网络流量等因素而异。 这可能会导致两次连续调用生成不同的列表。 只有同一网络中的服务器才列出。 广播数据包通常不会遍历路由器,正因为此,可能看不到列出的服务器,但它跨调用是稳定的。

列出的服务器不一定包含 IsClustered 和版本等其他信息。 这取决于列表的获取方式。 通过 SQL Server 浏览器服务列出的服务器比通过 Windows 基础结构发现的服务器具有更详细的信息,后者仅列出名称。

注释

现代 .NET 不支持代码访问安全性(CAS)或部分信任执行。 对于服务器发现,允许所需的SQL Server浏览器网络流量。

SQL Server 通过外部 Windows 服务 SQL Browser 为 SqlDataSourceEnumerator 提供信息。 默认情况下,此服务处于启用状态,但管理员可能会将其关闭或禁用,使服务器实例对此类不可见。

示例:

以下控制台应用程序检索有关可见SQL Server实例的信息,并在控制台窗口中显示信息。 安装 Microsoft.Data.SqlClient NuGet 包。

Imports Microsoft.Data.Sql
Imports System.Data

Module Module1
  Sub Main()
    ' Retrieve the enumerator instance and then the data.
    Dim instance As SqlDataSourceEnumerator = _
     SqlDataSourceEnumerator.Instance
    Dim table As System.Data.DataTable = instance.GetDataSources()

    ' Display the contents of the table.
    DisplayData(table)

    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
  End Sub

  Private Sub DisplayData(ByVal table As DataTable)
    For Each row As DataRow In table.Rows
      For Each col As DataColumn In table.Columns
        Console.WriteLine("{0} = {1}", col.ColumnName, row(col))
      Next
      Console.WriteLine("============================")
    Next
  End Sub
End Module
using Microsoft.Data.Sql;

class Program
{
  static void Main()
  {
    // Retrieve the enumerator instance and then the data.
    SqlDataSourceEnumerator instance =
      SqlDataSourceEnumerator.Instance;
    System.Data.DataTable table = instance.GetDataSources();

    // Display the contents of the table.
    DisplayData(table);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
  }

  private static void DisplayData(System.Data.DataTable table)
  {
    foreach (System.Data.DataRow row in table.Rows)
    {
      foreach (System.Data.DataColumn col in table.Columns)
      {
        Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
      }
      Console.WriteLine("============================");
    }
  }
}

另请参阅