我正在尝试使用SMO(VS 2010,SQL Server 2008)连接到SQL Server并查看服务器协议配置。我可以连接并列出服务和ClientProtocols以及正在运行的MSSQLSERVER服务帐户。但是,ServerInstances集合为空。目标服务器上唯一的实例是默认(MSSQLSERVER),不应该在集合中吗?如何获取它的实例以便我可以检查ServerProtocols集合?这是我正在使用的代码:
class Program
{
static void Main(string[] args)
{
//machine hosting installed sql server instance
ManagedComputer host = new ManagedComputer("fully-qualified.host.name");
if (host.ServerInstances.Count != 0)
{
//why is this 0? Is it because only the DEFAULT instance exists?
Console.WriteLine("/////////////// INSTANCES ////////////////");
foreach (ServerInstance inst in host.ServerInstances)
{
Console.WriteLine(inst.Name);
}
}
Console.WriteLine("/////////////// SERVICES ////////////////");
// enumerate sql services (looking for MSSSQLSERVER)
foreach (Service svc in host.Services)
{
Console.WriteLine(svc.Name);
}
Console.WriteLine("/////////////// DETAILS ////////////////");
// get name of MSSQLSERVER instance from user (pick from list above)
Service mssqlserver = host.Services["MSSQLSERVER"];
// print service account: .\{account} == "local account", "LocalSystem", "NetworkService", {domain}\{account} == "domain account"
Console.WriteLine("Service Account: {0}", mssqlserver.ServiceAccount);
// get client protocols
foreach (ClientProtocol cp in host.ClientProtocols)
{
Console.WriteLine("{0} {1} ({2})", cp.Order, cp.DisplayName, cp.IsEnabled ? "Enabled" : "Disabled");
}
}
}
我也试过了:
Urn u = new Urn("ManagedComputer[@Name='fully-qualified.host.name']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']");
ServerProtocol tcp = host.GetSmoObject(u) as ServerProtocol;
if (tcp != null)
{
Console.WriteLine("{0}", tcp.DisplayName);
}
但是我收到一条错误消息,指出:“不支持子表达式。”任何想法有什么不对?
答案 0 :(得分:2)
要在ServerInstances列表中获取结果,您需要显式设置:
host.ConnectionSettings.ProviderArchitecture = ProviderArchitecture.Use64bit;
或
host.ConnectionSettings.ProviderArchitecture = ProviderArchitecture.Use32bit;
答案 1 :(得分:0)
所以我从来没有找到答案,但我想出了一个解决方法。
ManagedComputer host = new ManagedComputer(hostName);
Server server = new Server();
Console.WriteLine("TcpEnabled: {0}", server.TcpEnabled);
这让我得到了我需要的东西,我希望能够枚举ServerProtocols,但这将使我能够验证最低限度。