views:

530

answers:

4

Can anyone tell me how to get remote Sqlserver instances using c# and SMO or any api?

I have a remote server name "RemoteMC", which has 2 instances of sql server: "RemoteMc" and "RemoteMC\sqlexpress"

I try to get the instances in code like this:

Server srv=new Server("RemoteMC");
DataTable dt=SmoApplication.EnumAvailableSqlServer(true);

But it returns "Host\sqlexpress"

I don't know what went wrong. How can I get the result back as:

RemoteMC
RemoteMC\sqlexpress;

?

A: 

One way to do it is to read the registry entry ""Software\Microsoft\Microsoft SQL Server\Instance Names\SQL\" under Local Machine. Are you allowed to read registry in the your solution?

Prashanth
+4  A: 

The SmoApplication.EnumAvailableSqlServers method is what you're looking for. There are 3 overloads, and one of those takes a string parameter for the server name.

It returns a DataTable whose rows have fields like Version, name, IsLocal, etc.

You'll need to add a reference to the SMO libraries, and you'll probably want to have "using Microsoft.SqlServer.Management.Smo;" at the top of your C# file.

See http://www.sqldbatips.com/showarticle.asp?ID=34 for an intro to SQL SMO.

EDIT: Some code to address this particular problem (I have not compiled or run this):

DataTable dataTable = SmoApplication.EnumAvailableSqlServers(false);

foreach (DataRow dataRow in dataTable)
{
    if ((dataRow["Server"] as string) == "MyServerName")
        Console.WriteLine(dataRow["Instance"] as string);
}
Gary McGill
using that i am getting local Sql discovery only.How can i go for remote sqlDiscovery.means i can run the application then it should go for specified machine and retrieves the info to my local machine.if i use EnumAvailableSqlServers(true) it gives local only if i pass string that named instances of remote only coming if another 2 more exists o remote i wont get them.
Cute
Hmmm... you're right. I have 2 instances on my machine, one SQL 2000 instance called "MYMACHINE" and one SQL 2005 instance called "MYMACHINE\MYSQL2005". If I pass "MYMACHINE" I just get that one instance. I think the problem is that you can pass in either a server name OR an instance name - and in this case the server name also happens to be an instance name, and EnumAvailableServers is doing what it normally does for an instance name, i.e. to return info only about that instance. Yuk.
Gary McGill
As an alternative, you can use the overload that takes a boolean parameter, and pass in false (meaning NOT just local instances). That'll get you a big table with all the locally-accessible instances, and you can filter on the Server column. It's not ideal, since presumably it takes longer to build the list. But, it'll work.
Gary McGill
using the properties of dataTable i can get non local instances fine but i have to get both named and default instances of a particular machine.Suppose in this case if i give MyMachine as argument i have to get MyMachine and aswell as MyMachine\MySQL2005 both i need and other network instances i dont need how to do this Gary.....Any idea???
Cute
As per my comment above, I don't think you'll be able to pass in the machine name as an argument. Pass false. This'll give you ALL the instances (on ALL machines), and you'll have to examine each row of the results to see which server it's on and what the instance is called. If you call EnumAvailableSqlServers(false) and examine the rows in the data table returned, you'll hopefully see what I mean.
Gary McGill
I've added some sample code to my answer which hopefully should remove any ambiguity about what I'm suggesting.
Gary McGill
Thank Gary i will try it as u suggested and will intimate after done.Thanks for ur suggestion.
Cute
Thank u very much Gary it's working fine Great Help Thanks to Gary as wll as Stackoverflow
Cute
+1  A: 

Use the builtin way.

System.Data.Sql.SqlDataSourceEnumerator
leppie
A: 

hi,

EnumAvailableServers(false) is returning data table with zero records i.e not listing any instance on a sql 2008 cluster.......what could be the reason???