2

我想检索SQL Server Protocols Network Configuration

在此处输入图像描述

我尝试搜索 Windows 注册表或与此相关的任何配置文件,但我发现的唯一示例是用于 powershell Enable or Disable a Server Network Protocol (SQL Server PowerShell),并使用 .Net 类。

所以问题是:如何使用 delphi 检索 SQL Server 协议网络配置?

4

1 回答 1

5

您可以使用ServerNetworkProtocolWMI 类,也可以根据 SQL Server 版本连接到正确的命名空间。

  • SQL Server 2005 = 计算机管理
  • SQL Server 2008 = 计算机管理10
  • SQL Server 2012 = 计算机管理11

试试这个样本

{$APPTYPE CONSOLE}


uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

type
  TMSSQlServerType=(MSSQL2005, MSSQL2008, MSSQL2012);


procedure  GetServerNetworkProtocolInfo(SQlServerType: TMSSQlServerType; const InstanceName: string);
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
  MSSQLNameSpaces  : array [TMSSQlServerType] of string = ('ComputerManagement','ComputerManagement10','ComputerManagement11');
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, Format('root\Microsoft\SqlServer\%s',[MSSQLNameSpaces[SQlServerType]]), WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM ServerNetworkProtocol Where InstanceName="%s"', [InstanceName]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    Writeln(Format('Enabled                        %s',[String(FWbemObject.Enabled)]));// Boolean
    Writeln(Format('MultiIpConfigurationSupport    %s',[String(FWbemObject.MultiIpConfigurationSupport)]));// Boolean
    Writeln(Format('ProtocolDisplayName            %s',[String(FWbemObject.ProtocolDisplayName)]));// String
    Writeln(Format('ProtocolName                   %s',[String(FWbemObject.ProtocolName)]));// String
    Writeln;
    FWbemObject:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetServerNetworkProtocolInfo(MSSQL2008,'MSSQLSERVER');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.
于 2013-07-18T22:02:15.870 回答