Export (0) Print
Expand All

SqlTriggerContext Class

Provides contextual information about the trigger that was fired.

System.Object
  Microsoft.SqlServer.Server.SqlTriggerContext

Namespace:  Microsoft.SqlServer.Server
Assembly:  System.Data (in System.Data.dll)

public sealed class SqlTriggerContext

The SqlTriggerContext type exposes the following members.

  NameDescription
Public propertyColumnCountGets the number of columns contained by the data table bound to the trigger. This property is read-only.
Public propertyEventDataGets the event data specific to the action that fired the trigger.
Public propertyTriggerActionIndicates what action fired the trigger.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsUpdatedColumnReturns true if a column was affected by an INSERT or UPDATE statement.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The contextual information provided includes the type of action that caused the trigger to fire, which columns were modified in an UPDATE operation, and, in the case of a data definition language (DDL) trigger, an XML EventData structure (see "Transact-SQL Reference" in SQL Server Books Online) that describes the triggering operation.

An instance of SqlTriggerContext is available from the SqlContext class, when the code is running inside a trigger through the TriggerContext property.

The following example shows a SqlTriggerContext object being used to determine if an Insert action occurred. If a row was inserted into the user's table, the user name and real name are retrieved from the inserted row and then added to the UserNameAudit table.

[SqlTrigger(Name = @"UsersAudit", Target = "[dbo].[users]", Event = "FOR INSERT")]
public static void UsersAudit()
{
   // Get the trigger context. 
   string userName;
   string realName;
   SqlCommand command;
   SqlTriggerContext triggContext = SqlContext.TriggerContext;
   SqlDataReader reader;

   switch (triggContext.TriggerAction)
   {
      case TriggerAction.Insert:

      // Retrieve the connection that the trigger is using. 
      using (SqlConnection connection
         = new SqlConnection(@"context connection=true"))
      {
         connection.Open();

         // Get the inserted row.
         command = new SqlCommand(@"SELECT * FROM INSERTED;",
                                  connection);

         // Get the user name and real name of the inserted user.
         reader = command.ExecuteReader();
         reader.Read();
         userName = (string)reader[0];
         realName = (string)reader[1];
         reader.Close();

         // Insert the user name and real name into the auditing table.
         command = new SqlCommand(@"INSERT [dbo].[UserNameAudit] (userName, realName) " 
                  + @"VALUES (@userName, @realName);", connection);

         command.Parameters.Add(new SqlParameter("@userName", userName));
         command.Parameters.Add(new SqlParameter("@realName", realName));

         command.ExecuteNonQuery();               

      }

      break;
   }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft