using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace WMIProvEvents
{
class SQLWEPExample {
public static void Main(string[] args)
{
    string query =  @"SELECT * FROM DDL_EVENTS " ;
    
    //
    // Default namespace for default SQL Server instance
    // 
    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();
    }
	}
}

}
