Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 List of local servers

Author  Topic 

SQLServerExplorer
Starting Member

5 Posts

Posted - 2010-12-26 : 23:54:56
Hi,

I'm playing with writing some code which iterates through a server list and/or iterates through the databases in a specific server, generating stats or code along the way.

There's no problem with programmatically getting a list of databases and then tables, given a specific server name. But, aside from using SQLDMO or SQLCMD -L, I can't seem to figure out how to get the list of servers inside a program.

Based on responses to similar questions in other forums, one way to get a list of servers on the local machine is to use HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\LastConnect.

This seems to almost work, but there's an extra name in there - BVTSQL1. The command line sqlcmd -L agrees with the above registry key, except that it doesn't have that BVTSQL1 entry.

If I provide a connection string in the program, something like

sConnect := "Provider=SQLOLEDB;Data Source=" + sComputerName + ";Trusted_Connection=Yes;Initial Catalog=Master"

, and then try using

select srvname from sys.sysservers

, the program returns only the last server which was previously connected to.

Is there any way - without using powershell or SQLDMO or the command line - to programmatically get a list of servers on the local machine?


SQLServerExplorer
Starting Member

5 Posts

Posted - 2010-12-27 : 12:10:37
Looks like it might come down to some socket programming, according to

http://efreedom.com/Question/1-1076096/Jdbc-Driver-SQL-Server-Can-Search-Database-Instances-Network

I'll give this a shot. If anyone knows another way to programmatically obtain the list of servers in SQL Server, please post your insight, if you would, thanks.

Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-12-27 : 12:18:52
This must be too easy to be what you want!

Jim


sp_helpserver

From BooksOnLine:
Reports information about a particular remote or replication server, or about all servers of both types. Provides the server name, the network name of the server, the replication status of the server, the identification number of the server, and the collation name. Also provides time-out values for connecting to, or queries against, linked servers.


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-12-27 : 12:23:05
What language are you programming in? Why not use DMO?

If you Google "enumerate sql servers" you'll find lots of results. Here are two links that may be useful to you.

http://www.sqlteam.com/article/finding-sql-servers-running-on-a-network

http://blog.netnerds.net/2007/01/vbscript-enumerating-all-sql-servers-on-a-domain/
Go to Top of Page

SQLServerExplorer
Starting Member

5 Posts

Posted - 2010-12-27 : 14:32:42
..What language are you programming in? Why not use DMO..

Because, according to Microsoft, that interface will no longer be supported. Anyway, using the Registry, I'm able to get the server list.

I just can't figure out what the first server - BVTSQL1 - is. It's obviously not really a server, but just the first entry in

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\LastConnect

Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-12-27 : 15:43:36
I meant SMO...
Go to Top of Page

SQLServerExplorer
Starting Member

5 Posts

Posted - 2010-12-30 : 18:02:01
SMO is a .net API, so we probably won't use it.

Anyway, the registry method is not universal, unfortunately. The same registry keys that SQL Server generates in Vista, doesn't seem to exist in Windows 7. So this will have to be done by querying the 1433 port.
Go to Top of Page

SQLServerExplorer
Starting Member

5 Posts

Posted - 2011-01-02 : 09:59:37
I was using the wrong registry key, SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\LastConnect, and getting partial results.

The correct key, it seems, is SOFTWARE\Microsoft\Microsoft SQL Server, in the InstalledInstances entry.

Dumb mistake on my part, it should have been obvious.



Go to Top of Page
   

- Advertisement -