SqlTriggerContext Třída

Definice

Poskytuje kontextové informace o triggeru, který byl aktivován.

public ref class SqlTriggerContext sealed
public sealed class SqlTriggerContext
type SqlTriggerContext = class
Public NotInheritable Class SqlTriggerContext
Dědičnost
SqlTriggerContext

Příklady

Následující příklad ukazuje objekt, který SqlTriggerContext se používá k určení, zda Insert došlo k akci. Pokud byl do usertabulky vložen řádek, uživatelské jméno a skutečné jméno se načtou z vloženého řádku a pak se přidají do tabulky UserNameAudit .

[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;
   }
}
<SqlTrigger(Name:="UsersAudit", Target:="[dbo].[users]", Event:="FOR INSERT")> _
Public Shared Sub UsersAudit()
        
   Dim command As SqlCommand
   Dim triggContext As SqlTriggerContext
   Dim reader As SqlDataReader
   Dim userName As String
   Dim realName As String

         
   ' Get the trigger context.
   triggContext = SqlContext.TriggerContext        

   Select Case triggContext.TriggerAction
           
      Case TriggerAction.Insert

         ' Retrieve the connection that the trigger is using.
         Using connection As 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 = CType(reader(0), String)
            realName = CType(reader(1), String)

            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()
                 
          End Using
         
   End Select

End Sub

Poznámky

Zadané kontextové informace zahrnují typ akce, která způsobila aktivaci triggeru, které sloupce byly změněny v operaci UPDATE, a v případě triggeru jazyka DDL (Data Definition Language) strukturu XML EventData (viz Referenční informace k jazyku Transact-SQL), která popisuje operaci aktivace.

Instance SqlTriggerContext je k dispozici z SqlContext třídy , když kód běží uvnitř triggeru TriggerContext prostřednictvím vlastnosti .

Vlastnosti

ColumnCount

Získá počet sloupců obsažených v tabulce dat svázané s triggerem. Tato vlastnost je jen ke čtení.

EventData

Získá data události specifická pro akci, která aktivovala trigger.

TriggerAction

Určuje, jaká akce aktivovala trigger.

Metody

IsUpdatedColumn(Int32)

Vrátí, true pokud byl sloupec ovlivněn příkazem INSERT nebo UPDATE.

Platí pro