如何获取特定计算机的SqlInstances列表 [英] How to get the list of SqlInstances of a particular machine

查看:96
本文介绍了如何获取特定计算机的SqlInstances列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何使用c#和SMO或任何api获取远程Sqlserver实例吗?

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

我有一个远程服务器名称 RemoteMC,其中有2个实例sql server: RemoteMc和 RemoteMC\sqlexpress

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);

但它返回 Host\sqlexpress

But it returns "Host\sqlexpress"

我不知道出了什么问题。我怎么能得到这样的结果:

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


RemoteMC

RemoteMC\sqlexpress;

RemoteMC
RemoteMC\sqlexpress;

推荐答案

您正在寻找SmoApplication.EnumAvailableSqlServers 方法。有3个重载,其中一个重载使用 string 参数作为服务器名称。

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.

它返回 DataTable ,其行中包含诸如 Version name IsLocal 等。

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

您需要添加对SMO库的引用,您可能想要在C#文件的顶部具有 using Microsoft.SqlServer.Management.Smo;

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.

请参见< a href = http://www.sqldbatips.com/showarticle.asp?ID=34 rel = nofollow noreferrer> http://www.sqldbatips.com/showarticle.asp?ID=34

EDIT :一些用于解决此特定问题的代码(我尚未编译或运行):

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

编辑 :。行添加到foreach中以便可以编译。

EDIT:.Rows add to foreach so that it can compile.

DataTable dataTable = SmoApplication.EnumAvailableSqlServers(false);

foreach (DataRow dataRow in dataTable.Rows)
{
    if ((dataRow["Server"] as string) == "MyServerName")
        Console.WriteLine(dataRow["Instance"] as string);
}

这篇关于如何获取特定计算机的SqlInstances列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆