Q: How can I List all the SQL Servers on the network programatically
in .NET ?
Answer:
Well, the first solution that I would turn to is using
SQLDMO. The solution discussed here from the VB 6.0 world but also hold
good in the .NET world. This should be possible in VB.NET after creating
the RCW (interop component) for MS SQL DMO Object library (sqldmo.dll). I
suppose managed SQL DMO does not exist. I think "Yukon" would surely remove
this limitation with loads of new features ... :) ...
Dim i As Integer
Dim oNames As SQLDMO.NameList
Dim oSQLApp As SQLDMO.Application
Set oSQLApp = New
SQLDMO.Application
Set oNames = oSQLApp.ListAvailableSQLServers()
For i = 1 To oNames.Count
' do something with oNames.Item(i)
Next i
Going a step further, after getting the SQL Servers
you can enlist the databases on a particular Server. Here's the code ...
Dim oSQLServer As New
SQLDMO.SQLServer
Dim i As Integer
If txtUser.Text = "" And txtPassword.Text = "" Then
oSQLServer.LoginSecure = True
End If
oSQLServer.Connect cboServer.Text, txtUser.Text, txtPassword.Text
For i = 1 To oSQLServer.Databases.Count
With oSQLServer.Databases(i)
If Not .SystemObject Then
cboDB.AddItem .Name
End If
End With
Next i
PS: This code was submitted by
my associate(Manoj) at one of the .NET NewsGroups.
Download Source Code
|