SqlTriggerContext Sınıf

Tanım

Tetiklenen tetikleyici hakkında bağlamsal bilgiler sağlar.

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

Örnekler

Aşağıdaki örnekte, bir SqlTriggerContext eylemin oluşup oluşmadığını belirlemek için kullanılan bir Insert nesne gösterilmektedir. 'nin tablosuna userbir satır eklenmişse, eklenen satırdan kullanıcı adı ve gerçek ad alınır ve ardından UserNameAudit tablosuna eklenir.

[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

Açıklamalar

Sağlanan bağlamsal bilgiler tetikleyicinin tetiklenmesine neden olan eylem türünü, update işleminde hangi sütunların değiştirildiğini ve veri tanımlama dili (DDL) tetikleyicisi söz konusu olduğunda, tetikleme işlemini açıklayan bir XML EventData yapısını (bkz . Transact-SQL Başvurusu).

SqlTriggerContext Kod özelliği aracılığıyla bir tetikleyici içinde çalıştırıldığında örneği sınıfından TriggerContext kullanılabilirSqlContext.

Özellikler

ColumnCount

Tetikleyiciye bağlı veri tablosunun içerdiği sütun sayısını alır. Bu özellik salt okunur durumdadır.

EventData

Tetikleyiciyi tetikleyen eyleme özgü olay verilerini alır.

TriggerAction

Tetikleyiciyi tetikleyen eylemi gösterir.

Yöntemler

IsUpdatedColumn(Int32)

Bir sütunun INSERT veya UPDATE deyiminden etkilenip etkilenmediğini döndürür true .

Şunlara uygulanır