如何获取特定机器的SqlInstances列表
谁能告诉我如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SmoApplication.EnumAvailableSqlServers
方法就是您正在寻找的方法。 有 3 个重载,其中之一采用string
参数作为服务器名称。它返回一个
DataTable
,其行包含Version
、name
、IsLocal
等字段。您需要添加对 SMO 库的引用,您可能希望在 C# 文件的顶部添加“
using Microsoft.SqlServer.Management.Smo;
”。请参阅http://www.sqldbatips.com/showarticle.asp?ID=34 有关 SQL SMO 的介绍。
编辑:一些代码来解决这个特定问题(我没有编译或运行这个):
编辑:.Rows添加到foreach以便它可以编译。
The
SmoApplication.EnumAvailableSqlServers
method is what you're looking for. There are 3 overloads, and one of those takes astring
parameter for the server name.It returns a
DataTable
whose rows have fields likeVersion
,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.
使用内置的方式。
Use the builtin way.
一种方法是读取本地计算机下的注册表项“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?