using System;
using System.Data;
using System.Data.SqlClient;

namespace Dependency
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			SqlConnection conn = new SqlConnection("server=.;integrated security=sspi;database=pubs");
			SqlCommand cmd = new SqlCommand("select au_lname, au_fname from dbo.v1", conn);
      //cmd.CommandType = CommandType.StoredProcedure;

			try
			{
                SqlDependency.Start("server=.;integrated security=sspi;database=pubs");

				// create dependency associated with cmd
				SqlDependency depend = new SqlDependency(cmd);

				// register handler
				depend.OnChange += new OnChangeEventHandler(MyOnChanged);

				conn.Open();
				Console.WriteLine("before executereader");
				SqlDataReader rdr = cmd.ExecuteReader();

				while (rdr.Read())
					Console.WriteLine(rdr[0]);
        rdr.NextResult();
        while (rdr.Read())
          Console.WriteLine(rdr[0]);

				// Wait for invalidation to come through
				Console.WriteLine("Press Enter to continue");
				Console.ReadLine();

                SqlDependency.Stop("server=.;integrated security=sspi;database=pubs");
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				Console.WriteLine(e.StackTrace);
			}	
		}
		
		static void MyOnChanged(object caller, SqlNotificationEventArgs e)
		{
			Console.WriteLine("hit my on changed");
			Console.WriteLine("Source " + e.Source);
			Console.WriteLine("Type " + e.Type);
			Console.WriteLine("Info " + e.Info);
			Console.WriteLine(caller.GetType());
		}
		
	}
}
