Server list not returning correctly

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
I have this neat little method to find the SQL instances on a local machine and display them in a list.

Code:
Private serverItems As New List(Of String)

Public Sub CheckServers() Handles bgworker.DoWork
        Try


            Dim instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
            Dim table As System.Data.DataTable = instance.GetDataSources()
            For Each row As System.Data.DataRow In table.Rows
                If Not IsDBNull("ServerName") AndAlso Environment.MachineName.Equals(row("ServerName").ToString()) Then
                    Dim item As String = String.Empty
                    item = row("ServerName").ToString()
                    If Not IsDBNull(row("InstanceName")) OrElse Not String.IsNullOrEmpty(Convert.ToString(row("InstanceName")).Trim()) Then
                        item += "\" + Convert.ToString(row("InstanceName")).Trim()
                    End If

                    serverItems.Add(item.ToString())
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("No Servers Found.")
        End Try
    End Sub

One problem. My server is called "DEV1\SQLEXPRESS" but this method only returns "DEV1"

Any thoughts? Perhaps the server is named wrong?

Edit: References:

Imports System.Data.Sql
Imports System.ComponentModel
 

Darko

Senior Member
Joined
Jul 9, 2008
Messages
627
Step through your code and see what the value of item is before and after this line:

item += "" + Convert.ToString(row("InstanceName")).Trim()

then add Convert.ToString(row("InstanceName")).Trim() to quick watch to see what's happening there.

Why are you converting instance to string? It's already a string? And it won't have spaces?
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
Good call on the strings. I've commented out the check for servers and simply added it to the list. Still nothing. It's as if there are no Instances installed.

Code:
Public Sub CheckServers() Handles BackgroundWorker1.DoWork
        Try


            Dim instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
            Dim table As System.Data.DataTable = instance.GetDataSources()
            For Each row As System.Data.DataRow In table.Rows
                If Not IsDBNull("ServerName") AndAlso Environment.MachineName.Equals(row("ServerName")) Then
                    Dim item As String = String.Empty
[B]                    item = row("ServerName")
                    'If Not IsDBNull(row("InstanceName")) OrElse Not String.IsNullOrEmpty(Convert.ToString(row("InstanceName")).Trim()) Then
                    item += "" + (row("InstanceName"))
                    'End If[/B]

                    serverItems.Add(item.ToString())
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("No Servers Found.")
        End Try
    End Sub
 

LPCPT

Expert Member
Joined
Jun 24, 2011
Messages
1,035
This may be a long shot, but is backslash not an escape character. In C# I used to make it double backslash. Like when you use filepaths etc.
Even typing it in here looses the "backslash" character
 
Last edited:

Alton Turner Blackwood

Honorary Master
Joined
Apr 30, 2010
Messages
27,483
This may be a long shot, but is backslash not an escape character. In C# I used to make it double backslash. Like when you use filepaths etc.
Even typing it in here looses the "backslash" character
This man ^ knows his schit!
 
Last edited:

schuits

Expert Member
Joined
Mar 7, 2013
Messages
1,951
It works fine when I try it. Only I removed
Code:
If Not IsDBNull("ServerName") AndAlso Environment.MachineName.Equals(row("ServerName").ToString()) Then
Shouldn't this be

Code:
If Not IsDBNull(row("ServerName")) AndAlso Environment.MachineName.Equals(row("ServerName").ToString()) Then
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
Have you tried C#?

Not sure the outcome would be any different. It seems to be a SQL 2014 issues. Something to do with SMO's and machine objects, some or other jargon like that.

It works fine when I try it. Only I removed
Code:
If Not IsDBNull("ServerName") AndAlso Environment.MachineName.Equals(row("ServerName").ToString()) Then
Shouldn't this be

Code:
If Not IsDBNull(row("ServerName")) AndAlso Environment.MachineName.Equals(row("ServerName").ToString()) Then

Works fine over a network. Picks up all the server names + instance names. On a local machine though its balls. Only picks up the server name "DEV1/" and not "DEV1/SQLEXPRESS"
 
Top