예제: .NET Framework에서 WMI 이벤트 공급자 사용

다음 예에서는 WMI 이벤트 공급자를 사용하여 SQL Server의 기본 설치 인스턴스에서 발생하는 모든 DDL 이벤트에 대한 이벤트 데이터를 반환하는 응용 프로그램을 C#으로 만듭니다.

이 예는 다음 명령 파일을 사용하여 컴파일됩니다.

set compiler_path=C:\WINNT\Microsoft.NET\Framework\v2.0.50110

%compiler_path%\csc %1

using System;
using System.Management;

class SQLWEPExample 
{
    public static void Main(string[] args)
    {
        string query =  @"SELECT * FROM DDL_EVENTS " ;
        // Default namespace for default instance of SQL Server 
        string managementPath =
            @"\\.\root\Microsoft\SqlServer\ServerEvents\MSSQLSERVER";
        ManagementEventWatcher  watcher = 
            new ManagementEventWatcher(new WqlEventQuery (query));
        ManagementScope scope = new ManagementScope (managementPath);
        scope.Connect();
        watcher.Scope = scope;
        Console.WriteLine("Watching...");
        while (true)
        {
            ManagementBaseObject obj = watcher.WaitForNextEvent();
            Console.WriteLine("Event!");
            foreach (PropertyData data in obj.Properties)
            {
                Console.Write("{0}:", data.Name);
                if (data.Value == null)
                {
                    Console.WriteLine("<null>");
                }
                else
                {
                    Console.WriteLine(data.Value.ToString());
                }
            }
            Console.WriteLine("-----");
            Console.WriteLine();
            Console.WriteLine();
        }
    }
}

참고 항목

개념

서버 이벤트용 WMI 공급자 개념