在LAN上的其他计算机上查找SQL Server实例

时间:2011-08-08 05:21:02

标签: sql-server vb.net

我有以下函数来查找SQL Server实例。它在本地计算机上工作正常。您能告诉我如何使用此功能检查局域网中其他计算机上的实例吗?我正在使用VS 2008(.NET Framework 3.5)和SQL Server Express 2005。

Private Function MyInstanceFound(ByVal MyInstanceName As String) As Boolean
    Dim InstanceFound As Boolean = False
    Dim MC As ManagedComputer = New ManagedComputer()
    For Each SI As ServerInstance In MC.ServerInstances
        If SI.Name.ToString = MyInstanceName Then
            InstanceFound = True
            Exit For
        End If
    Next
    Return InstanceFound
End Function

提前谢谢。

此致 SKPaul

2 个答案:

答案 0 :(得分:0)

您需要使用另一个ManagedComputer构造函数,如下所示:

public ManagedComputer (
    string machineName
)

ManagedComputer Constructor (String)

答案 1 :(得分:0)

Public Shared Function GetServerList(ByVal cmbServers As ComboBox)
    Dim Server As String = String.Empty
    Dim instance As Sql.SqlDataSourceEnumerator = Sql.SqlDataSourceEnumerator.Instance
    Dim table As System.Data.DataTable = instance.GetDataSources()
    For Each row As System.Data.DataRow In table.Rows
        Server = String.Empty
        Server = row("ServerName")
        If row("InstanceName").ToString.Length > 0 Then
            Server = Server & "\" & row("InstanceName")
        End If
        cmbServers.Items.Add(Server)
    Next
    cmbServers.SelectedIndex = cmbServers.FindStringExact(Environment.MachineName)

End Function

在表单中添加组合框名称txtservidores。通话功能 GetServerList(txtServidores)