List of Sqlservers on LAN

  • Thread starter Thread starter Reza Alirezaei
  • Start date Start date
R

Reza Alirezaei

How can I have a list of all sql servers avaiable on our LAN populated in a
DropDownList exacly like what we have when we want to specify Datasource in
ADO.Net or wherever?

Thanks
 
Hello,

Easiest thing to do IMHO is to make a ref to the SQLDMO.dll and call the
method that follows :

SQLDMO.Application app = new SQLDMO.ApplicationClass();

SQLDMO.NameList lst = app.ListAvailableSQLServers();

IEnumerator ie = lst.GetEnumerator();

while (ie.MoveNext())

{

Debug.WriteLine(ie.Current.ToString());

}

HTH,



Alex
 
that works fine,,,how can I get a list of databases available on the
specified server on a separate Combobox?
 
it returns a collection called a NameList, just use foreach.

private SQLDMO.NameList sqlServers;

sqlDMOApp = new SQLDMO.Application();

sqlServers = sqlDMOApp.ListAvailableSQLServers();

foreach (string server in sqlServers)

{

comboBox1.Items.Add(server);

}
 
look up the sp_databases stored procedure..

//**
using System.Data;
using System.Data.SqlClient;

private void ListDatabases(string serverName)
{
SqlConnection cn = new SqlConnection("Integrated Security=SSPI;Persist
Security Info=False;User ID=sa;Initial Catalog=te;Data Source=" + serverName
+ ";");

try
{
cn.Open();
SqlDataReader dr = (new SqlCommand("sp_databases",
cn)).ExecuteReader();
}
catch
{
if (cn.State == ConnectionState.Open) {cn.Close();}
return;
}

Console.WriteLine("List of [" + serverName + "] SQL Server" );
while (dr.Read())
{
Console.WriteLine(dr["DATABASE_NAME"] as string);
}

cn.Close();
}
//***
 
doh, didnt see "databases".

Zoury has it right, also if you want to get more detailed information you
can just hit the sysdatabases table in the master databases, pretty much all
sp_databases is doing.

select * from master.dbo.sysdatabases

Your real problem is that your choice is based off the server selection they
make and the security context your going to connect as. You can either make
them select the server, then enter security info, then pass them as
variables to the sqlconnection string, OR embed trusted or a sql login that
you know has rights to the servers (kinda dangerous).

Zoury said:
look up the sp_databases stored procedure..

//**
using System.Data;
using System.Data.SqlClient;

private void ListDatabases(string serverName)
{
SqlConnection cn = new SqlConnection("Integrated Security=SSPI;Persist
Security Info=False;User ID=sa;Initial Catalog=te;Data Source=" + serverName
+ ";");

try
{
cn.Open();
SqlDataReader dr = (new SqlCommand("sp_databases",
cn)).ExecuteReader();
}
catch
{
if (cn.State == ConnectionState.Open) {cn.Close();}
return;
}

Console.WriteLine("List of [" + serverName + "] SQL Server" );
while (dr.Read())
{
Console.WriteLine(dr["DATABASE_NAME"] as string);
}

cn.Close();
}
//***

--
Best Regards
Yanick Lefebvre
Reza Alirezaei said:
that works fine,,,how can I get a list of databases available on the
specified server on a separate Combobox?

populated
in
 
Back
Top