title: Trigger Windows Service when the new record insert in to DB
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/877273/change-notification-with-sql-server-2008">Change Notification with Sql Server 2008</a>  </p>
  </blockquote>
  
  
  
  <p>Am just wondering is there's anyway i can write a windows service in C# that will be trigger when the new record get inserted into Database.</p>
  
  <p>And i would like to connect DB thru wcf also. Please give any ideas or suggestion.</p>
  
  <p>Thanks in Advance. </p>
  
  <p>Based on demo.b Instruction,here is the code.</p>
  
  <p>SQL Database Details</p>
  
  <hr>
  
  <p>My Database Name : MyWeb,Table Name : StoryItems,
  Columns: Location,Headline,Name,Genre.</p>
  
  <hr>
  
  <pre><code> public partial class Triggers
  {
      // Enter existing table or view for the target and uncomment the attribute line
      [Microsoft.SqlServer.Server.SqlTrigger(Name = "Trigger_Web", Target = "StoryItems", Event = "FOR INSERT")]
      public static void Trigger_Web()
      {
  
      SqlCommand command;
      SqlTriggerContext triggerContext = SqlContext.TriggerContext;
      SqlPipe pipe = SqlContext.Pipe;
      SqlDataReader reader;
  
      if (triggerContext.TriggerAction == TriggerAction.Insert)
      {
          using (SqlConnection connection = new SqlConnection(@"context connection=true"))
          {
              connection.Open();
              command = new SqlCommand(@"SELECT * FROM StoryItems", connection);
              reader = command.ExecuteReader();
              reader.Read();
  
              // get inserted value
              // ***********Here am trying to retrieve the location and name column value            
              Location= (string)reader[9];
              Name= (String) reader[9];
              reader.Close();
              try
              {
                  // try to pass parameter to windows service
  
                  WindowsService param = new WindowService(InsertedValue1, InsertedValue2);
              }
              catch (Exception ex)
              {
  
              }
  
  
  
          // Replace with your own code
          SqlContext.Pipe.Send("Trigger FIRED");
        }
      }
  }
  }
  </code></pre>
  
  <p>Some how it doesn't like column name, am not sure what am missing here."Trigger_Web" is my CLR SP name.</p>