How do you enumerate SQL Servers in .NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like my application to be able to enumerate all available SQL servers
for the user to pick where they want the database created. The Wizard for
Sql data access does this. I just want to enumerate a list so I can put them
in a drop down box. I'm really at a loss of where to go to get this list. I
assume WMI would provide it, but is that the best way. And if so, what class
is it in?
 
I would like my application to be able to enumerate all available SQL servers
for the user to pick where they want the database created.
I just want to enumerate a list so I can put them in a drop down box.

I'd use SQL-DMO - just add a reference to the COM object "Microsoft
SQLDMO Object Library" (sqldmo.dll) and then you're good to go:

public ArrayList FillListOfServers()
{
ArrayList alsServers = new ArrayList();

SQLDMO.NameList oNames;
SQLDMO.Application oSQLApp;

try
{
oSQLApp = new SQLDMO.Application();
oNames = oSQLApp.ListAvailableSQLServers();

for (int i = 1; i <= oNames.Count; i++)
{
if (!alsServers.Contains(oNames.Item(i)))
{
alsServers.Add(oNames.Item(i));
}
}
oNames = null;
oSQLApp = null;
}
catch
{
// handle exception
}
}

SQL-DMO is installed as part of SQL Server, when you install it on
your machine.

Marc
 
Use DMO and Interop. Once SQL Server 2005 is out, you will be able to use SMO
in .NET, but DMO and Interop is the only way right now. You will have to
create a .NET wrapper to call the DMO classes.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Back
Top