using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Transactions;


public partial class Triggers
{
  // Enter existing table or view for the target and uncomment the attribute line
  [Microsoft.SqlServer.Server.SqlTrigger (Name="AddToObsolete", Target="jobs", Event="FOR DELETE")]
  public static void AddToObsolete()
  {
    // get the trigger context so you can tell what is
    // going on
    SqlTriggerContext tc = SqlContext.TriggerContext;

    // this trigger is for deletes
    if (tc.TriggerAction == TriggerAction.Delete)
    {
      // Make a command
      SqlConnection conn = new SqlConnection("context connection=true");
      conn.Open();
      SqlCommand cmd = conn.CreateCommand();
      // make a command that inserts the deleted row into
      // the obsoletejobs table
      cmd.CommandText = "insert into obsoletejobs select * from deleted";

      // move the rows
      int rowsAffected = (int)cmd.ExecuteNonQuery();

      if (rowsAffected == 0)
      {
        // something bad happened, roll back
        Transaction.Current.Rollback();
      }
    }
  }

}
