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:
Public Sub GetRDBMSServerList(ByVal ServerList As ComboBox) Try Dim Server As String = String.Empty Dim Instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance Dim ServerDt As DataTable = Instance.GetDataSources() For Each ServerInstance As DataRow In ServerDt.Rows Server = String.Empty Server = CStr(ServerInstance("ServerName")) If ServerInstance("InstanceName").ToString.Length > 0 Then Server = Server & "\" & CStr(ServerInstance("InstanceName")) End If With ServerList .AutoCompleteMode = AutoCompleteMode.SuggestAppend .AutoCompleteSource = AutoCompleteSource.ListItems .DroppedDown = False .Editable = True .Items.Add(Server) End With Next ServerInstance ServerList.SelectedIndex = ServerList.FindStringExact(Environment.MachineName) Catch ArgEx As ArgumentException MessageBox.Show(ArgEx.ToString(), _ "Argument Exception", _ MessageBoxButtons.OK, _ MessageBoxIcon.Error, _ MessageBoxDefaultButton.Button1) End Try 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:
GetRDBMSServerList(RDBMSList)
You will also need to add two references to your code pane.
Code:Imports System.Data.Sql Imports System.Data


Reply With Quote