Results 1 to 1 of 1

Thread: VS2005: Get MSSQL server list on network

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Post VS2005: Get MSSQL server list on network

    If you need to obtain a list of MSSQL servers on your network, use this code snippet to add the items to a combobox.

    vb Code:
    1. Public Sub GetRDBMSServerList(ByVal ServerList As ComboBox)
    2.         Try
    3.             Dim Server As String = String.Empty
    4.             Dim Instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
    5.             Dim ServerDt As DataTable = Instance.GetDataSources()
    6.  
    7.             For Each ServerInstance As DataRow In ServerDt.Rows
    8.                 Server = String.Empty
    9.                 Server = CStr(ServerInstance("ServerName"))
    10.  
    11.                 If ServerInstance("InstanceName").ToString.Length > 0 Then
    12.                     Server = Server & "\" & CStr(ServerInstance("InstanceName"))
    13.                 End If
    14.  
    15.                 With ServerList
    16.                     .AutoCompleteMode = AutoCompleteMode.SuggestAppend
    17.                     .AutoCompleteSource = AutoCompleteSource.ListItems
    18.                     .DroppedDown = False
    19.                     .Editable = True
    20.                     .Items.Add(Server)
    21.                 End With
    22.             Next ServerInstance
    23.  
    24.             ServerList.SelectedIndex = ServerList.FindStringExact(Environment.MachineName)
    25.         Catch ArgEx As ArgumentException
    26.             MessageBox.Show(ArgEx.ToString(), _
    27.                             "Argument Exception", _
    28.                             MessageBoxButtons.OK, _
    29.                             MessageBoxIcon.Error, _
    30.                             MessageBoxDefaultButton.Button1)
    31.         End Try
    32.     End Sub

    If you have a ComboBox called RDBMSList, the code is not needed to be modified, otherwise you will have to pass the name of the ComboBox to the sub.
    vb Code:
    1. GetRDBMSServerList(RDBMSList)

    You will also need to add two references to your code pane.
    Code:
    Imports System.Data.Sql
    Imports System.Data

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width