SqlTriggerContext Klasa

Definicja

Zawiera kontekstowe informacje o wyzwalaczu, który został wyzwolony.

public ref class SqlTriggerContext sealed
public sealed class SqlTriggerContext
type SqlTriggerContext = class
Public NotInheritable Class SqlTriggerContext
Dziedziczenie
SqlTriggerContext

Przykłady

W poniższym przykładzie pokazano SqlTriggerContext obiekt używany do określenia, czy wystąpiła Insert akcja. Jeśli wiersz został wstawiony do usertabeli , nazwa użytkownika i rzeczywista nazwa zostaną pobrane z wstawionego wiersza, a następnie dodane do tabeli 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

Uwagi

Podane informacje kontekstowe zawierają typ akcji, która spowodowała wyzwolenie wyzwalacza, które kolumny zostały zmodyfikowane w operacji UPDATE, a w przypadku wyzwalacza języka definicji danych (DDL) struktura XML EventData (zobacz Dokumentacja języka Transact-SQL), która opisuje operację wyzwalającą.

Wystąpienie klasy SqlTriggerContext jest dostępne w SqlContext klasie, gdy kod jest uruchomiony wewnątrz wyzwalacza za pośrednictwem TriggerContext właściwości .

Właściwości

ColumnCount

Pobiera liczbę kolumn zawartych w tabeli danych powiązanej z wyzwalaczem. Ta właściwość jest tylko do odczytu.

EventData

Pobiera dane zdarzenia specyficzne dla akcji, która wyzwoliła wyzwalacz.

TriggerAction

Wskazuje, jaka akcja została wyzwolona przez wyzwalacz.

Metody

IsUpdatedColumn(Int32)

Zwraca wartość true , jeśli na kolumnę ma wpływ instrukcja INSERT lub UPDATE.

Dotyczy