Delen via


SqlTriggerAttribute Class

Definition

Used to mark a method definition in an assembly as a trigger in SQL Server. The properties on the attribute reflect the physical attributes used when the type is registered with SQL Server. This class cannot be inherited.

public ref class SqlTriggerAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
[System.Serializable]
public sealed class SqlTriggerAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
[<System.Serializable>]
type SqlTriggerAttribute = class
    inherit Attribute
Public NotInheritable Class SqlTriggerAttribute
Inherits Attribute
Inheritance
SqlTriggerAttribute
Attributes

Examples

The following example shows the SqlTriggerAttribute specifying the name of the trigger, the target table, and the event that will fire the trigger. See SqlTriggerContext for the full trigger example.

[SqlTrigger(Name = @"SalesAudit", Target = "[dbo].[SalesInfo]", Event = "FOR INSERT")]
public static void SalesAudit()
{
   // Get the trigger context.
   SqlTriggerContext triggContext = SqlContext.TriggerContext;

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

      // Do something in response to the INSERT.

      break;
   }
}
<SqlTrigger(Name:="SalesAudit", Target:="[dbo].[SalesInfo]", Event:="FOR INSERT")> _
Public Shared Sub SalesAudit()
        
   Dim triggContext As SqlTriggerContext
         
   ' Get the trigger context.
   triggContext = SqlContext.TriggerContext        

   Select Case triggContext.TriggerAction
      Case TriggerAction.Insert
      
      ' Do something in response to the INSERT.
         
   End Select

End Sub

Remarks

See "CLR Triggers" in SQL Server 2005 Books Online for more information on CLR triggers and examples.

Constructors

SqlTriggerAttribute()

An attribute on a method definition in an assembly, used to mark the method as a trigger in SQL Server.

Properties

Event

The type of trigger and what data manipulation language (DML) action activates the trigger.

Name

The name of the trigger.

Target

The table to which the trigger applies.

Applies to