如何检索SQL Server协议网络配置?

时间:2013-07-18 21:49:25

标签: sql-server delphi

我想检索SQL Server Protocols Network Configuration

enter image description here

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

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

1 个答案:

答案 0 :(得分:5)

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

  • SQL Server 2005 = ComputerManagement
  • SQL Server 2008 = ComputerManagement10
  • SQL Server 2012 = ComputerManagement11

试试这个样本

{$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.