Reputation: 5305
How can I extract the list of available SQL servers in an SQL server group? I'm planning to put that list in a combo box in VB.NET.
Upvotes: 3
Views: 11934
Reputation: 1622
If you didn't want to be tied to SQL SMO, which is what Ben's article uses, you can do something like this to discover all SQL servers on your network:
Private Sub cmbServer_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbServer.DropDown
Dim oTable As Data.DataTable
Dim lstServers As List(Of String)
Try
If cmbServer.Items.Count = 0 Then
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
oTable = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources
For Each oRow As DataRow In oTable.Rows
If oRow("InstanceName").ToString = "" Then
cmbServer.Items.Add(oRow("ServerName"))
Else
cmbServer.Items.Add(oRow("ServerName").ToString & "\" & oRow("InstanceName").ToString)
End If
Next oRow
End If
Catch ex As Exception
ErrHandler("frmLogin", "cmbServer_DropDown", ex.Source, ex.Message, Ex.InnerException)
Finally
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
If oTable IsNot Nothing Then
oTable.Dispose()
End If
End Try
End Sub
The SqlDataSourceEnumerator class is nice because it gives you SQL server discovery right out of the 2.0 framework.
Upvotes: 5
Reputation: 605
In C# I've used calls to odbc32.dll
For example:
[DllImport("odbc32.dll", CharSet = CharSet.Ansi)]
private static extern short SQLBrowseConnect(
IntPtr hconn, StringBuilder inString,
short inStringLength, StringBuilder outString, short outStringLength, out short
outLengthNeeded);
Documentation for that function is on MSDN
Upvotes: 0
Reputation: 103385
The only way I knew to do it was using the command line:
osql -L
But I found the below article which seems to solve your specific goal filling a combobox:
http://www.sqldbatips.com/showarticle.asp?ID=45
Upvotes: 5