sql-serverwindows-networking

Browsing for SQL Servers


I'm writing a database application that connects to SQL server. I'd like to implement a similar connection dialog box like the one in SQL Management Studio. I've already found a way to get the list of databases on a server, but I'd really like to get the list of available servers on the network so end users won't have to type in the name/IP of the server.


Solution

  • From here:

    CREATE PROCEDURE dbo.ListLocalServers 
    AS 
    BEGIN 
        SET NOCOUNT ON 
    
        CREATE TABLE #servers(sname VARCHAR(255)) 
    
        INSERT #servers EXEC master..XP_CMDShell 'OSQL -L' 
        -- play with ISQL -L too, results differ slightly 
    
        DELETE #servers WHERE sname='Servers:' 
    
        SELECT LTRIM(sname) FROM #servers WHERE sname != 'NULL' 
    
        DROP TABLE #servers 
    END
    

    Alternative SQL DMO method here