By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,172 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,172 IT Pros & Developers. It's quick & easy.

List of all available sql servers...

P: 45
Hi all,

I am trying to get a list of all the available sql servers and then populate them in a combo box. I do this with the following code taken from http://www.csharphelp.com/archives2/archive342.html

SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();

for (int i = 0; i < sqlServers.Count; i++)
{
object srv = sqlServers.Item(i + 1);
if (srv != null)
{
this.cmbServers.Items.Add(srv);
}
}

if (this.cmbServers.Items.Count > 0)
{
this.cmbServers.SelectedIndex = 0;
}
else
{
this.cmbServers.Text = "<No available SQL Servers>";
}

However, I have a little problem. The above code retrieves a list of all the available servers, but for the local server it returns "(local)".
So, I get the Item (local) instead of the servername of my local server in the Combo Box. I need to get the servername entry in stead of "(local)"

How do I get that??

Many thanks.
May 8 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 218
Hi

We have a routine which looks like this:-
Expand|Select|Wrap|Line Numbers
  1. Private Sub GetDbsNames()
  2. On Error GoTo ErrorConnection
  3. Dim Rs As New ADODB.Recordset
  4. Dim Cnn As New ADODB.Connection
  5.  
  6.     With Cnn
  7.         .Provider = "SQLOLEDB"
  8.         .Properties![Data Source] = ServerName
  9.         .Properties![User Id] = UserName
  10.         .Properties![Password] = Nz(Password, "")
  11.         .Properties![Initial Catalog] = "Master"
  12.         .Open
  13.     End With
  14.     Rs.Open "select name from master.dbo.sysdatabases", Cnn
  15.     Rs.MoveFirst
  16.     Do Until Rs.EOF
  17.     If Rs!name = "Test" Then
  18.         Database.AddItem Rs!name, 0
  19.     Else
  20.         Database.AddItem Rs!name
  21.     End If
  22.     Rs.MoveNext
  23.     Loop
  24.     Rs.Close
  25.     Cnn.Close
  26.     Set Rs = Nothing
  27.     Set Cnn = Nothing
  28.     Exit Sub
  29.  
  30. ErrorConnection:
  31.     Set Cnn = Nothing
  32.  
  33. End Sub
HTH
Steve
May 8 '07 #2

P: 45
Thanks Steve,

Although it is a good solution, but I am looking to populate all the sql servers before I connect to any of them.
May 10 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.