using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;

namespace Test1
{
  class Program
  {
    static void Main(string[] args)
    {
      using (SqlConnection conn = new SqlConnection("database=pubs;server=.;integrated security=sspi"))
      using (SqlCommand cmd = new SqlCommand("select job_id, job_desc from dbo.jobs", conn))
      {
        try
        {
          conn.Open();
          SqlNotificationRequest req = new SqlNotificationRequest();
          req.UserData = Guid.NewGuid().ToString();
          req.Options = "service=myservice;local database=pubs";
          cmd.Notification = req;
          SqlDataReader rdr = cmd.ExecuteReader();
          while (rdr.Read())
            Console.WriteLine(rdr[1]);
          rdr.Close();
          SqlCommand waiter = new SqlCommand();
          waiter.Connection = conn;
          cmd.Notification = null;
          cmd.CommandText = "waitfor (receive top(1) cast(message_body as xml) from myq)";
          string s = (string)cmd.ExecuteScalar();
          Console.WriteLine(s);
        }
        catch (Exception ex)
        {
          Console.WriteLine(ex.Message);
        }
      }

    }
  }
}
