SqlTriggerContext 클래스

정의

발생한 트리거에 대한 컨텍스트 정보를 제공합니다.

public ref class SqlTriggerContext sealed
public sealed class SqlTriggerContext
type SqlTriggerContext = class
Public NotInheritable Class SqlTriggerContext
상속
SqlTriggerContext

예제

다음 예제와 SqlTriggerContext 확인 하는 데 사용 되 고 개체는 Insert 동작이 발생 합니다. 행에 삽입 된 경우는 user의 테이블, 사용자 이름 및 실제 이름을 삽입 된 행에서 검색 되며 그런 다음에 추가 합니다 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

설명

제공 된 컨텍스트 정보를 포함 열, 수정 된 업데이트 작업에서 데이터 정의 언어의 경우 (DDL) 트리거는 EventData XML 구조는 트리거를 발생 시킨 동작 유형 (참조 Transact-SQL 참조)는 트리거 작업을 설명 합니다.

인스턴스의 SqlTriggerContext 에서 사용할 수는 SqlContext 클래스를 통해 트리거 내에서 코드를 실행 중일 때를 TriggerContext 속성입니다.

속성

ColumnCount

트리거에 바인딩된 데이터 테이블에 포함된 열의 수를 가져옵니다. 이 속성은 읽기 전용입니다.

EventData

트리거를 발생시킨 작업과 관련된 이벤트 데이터를 가져옵니다.

TriggerAction

트리거를 발생시킨 작업을 나타냅니다.

메서드

IsUpdatedColumn(Int32)

열이 INSERT 또는 UPDATE 문의 영향을 받았으면 true를 반환합니다.

적용 대상