如何获取特定机器的SqlInstances列表

发布于 2024-07-26 15:22:52 字数 430 浏览 3 评论 0原文

谁能告诉我如何使用 C# 和 SMO 或任何 api 获取远程 Sqlserver 实例?

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

我尝试在如下代码中获取实例:

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

但它返回“Host\sqlexpress”

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

远程MC
RemoteMC\sqlexpress;

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;

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜无邪 2024-08-02 15:22:52

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

它返回一个 DataTable,其行包含 VersionnameIsLocal 等字段。

您需要添加对 SMO 库的引用,您可能希望在 C# 文件的顶部添加“using Microsoft.SqlServer.Management.Smo;”。

请参阅http://www.sqldbatips.com/showarticle.asp?ID=34 有关 SQL SMO 的介绍。

编辑:一些代码来解决这个特定问题(我没有编译或运行这个):

编辑:.Rows添加到foreach以便它可以编译。

DataTable dataTable = SmoApplication.EnumAvailableSqlServers(false);

foreach (DataRow dataRow in dataTable.Rows)
{
    if ((dataRow["Server"] as string) == "MyServerName")
        Console.WriteLine(dataRow["Instance"] as 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.

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

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);
}
看海 2024-08-02 15:22:52

使用内置的方式。

System.Data.Sql.SqlDataSourceEnumerator

Use the builtin way.

System.Data.Sql.SqlDataSourceEnumerator
情丝乱 2024-08-02 15:22:52

一种方法是读取本地计算机下的注册表项“Software\Microsoft\Microsoft SQL Server\Instance Names\SQL\”。您是否允许在解决方案中读取注册表?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文