Getting a list of SQL servers on a network

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

Guest

I want to have a lookup window in my app so that users can select the SQL server the app is going to connect to. What's the best way to do this?
 
MK, I'm actually writing a control that will do this for you... drop me a
line if you want a copy of it, I can send you the source , control or
both...the real stuff all works, I'm just putting some finishing touches on
some visual elements.
drop me a line at wryan AT infoprogroup DotCom
M K said:
I want to have a lookup window in my app so that users can select the SQL
server the app is going to connect to. What's the best way to do this?
 
Using the SQLDMO library you can use the following code to populate a dropdown list

//get all available SQL Server
SQLDMO.NameList sqlServers
tr

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

catch (Exception err

MessageBox.Show(err.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
return


for(int i=0; i < sqlServers.Count; i++)
{
object srv = sqlServers.Item(i + 1);
if(srv != null)
{
this.cboSqlServers.Items.Add(srv);
}
}
if(this.cboSqlServers.Items.Count > 0)
this.cboSqlServers.SelectedIndex = 0;
else
this.cboSqlServers.Text = "<No available SQL Servers>";

And then to get a list of databases for the selected server

//get all available databases from an SQL Server
tr

SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
srv.Connect(this.cboSqlServers.SelectedItem.ToString(),this.txtUserName.Text,this.txtPassword.Text);
foreach(SQLDMO.Database db in srv.Databases)
{
if(db.Name!=null)
this.cboDatabases.Items.Add(db.Name);


if(this.cboDatabases.Items.Count > 0)
this.cboDatabases.SelectedIndex = 0;


catch (Exception err

MessageBox.Show(err.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
 
The SQL DMO method works... but this is for an install app. SQL DMO requires some registration of components.
 
Hi Mark,

You consider the NetServerEnum API function to list avaliable SQL servers.
For more information , you can refer to:

HOWTO: List Servers in 32-bit Visual Basic Using NetServerEnum (Q198896)

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q198896


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/net
mgmt/netserverenum.asp

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top