SqlTriggerContext SqlTriggerContext SqlTriggerContext SqlTriggerContext Class

定义

提供所激发的触发器的上下文信息。Provides contextual information about the trigger that was fired.

public ref class SqlTriggerContext sealed
public sealed class SqlTriggerContext
type SqlTriggerContext = class
Public NotInheritable Class SqlTriggerContext
继承
SqlTriggerContextSqlTriggerContextSqlTriggerContextSqlTriggerContext

示例

下面的示例演示了SqlTriggerContext一个对象, 该对象用于确定Insert是否发生了某个操作。The following example shows a SqlTriggerContext object being used to determine if an Insert action occurred. 如果在user的表中插入了一行, 则将从插入的行中检索用户名和真实名称, 然后将其添加到UserNameAudit表中。If a row was inserted into the user's table, the user name and real name are retrieved from the inserted row and then added to the UserNameAudit table.

[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

注解

提供的上下文信息包括导致触发器激发的操作类型、在更新操作中修改的列以及 XML EventData 结构 (在数据定义语言 (DDL) 触发器的情况下)。 引用), 用于描述触发操作。The contextual information provided includes the type of action that caused the trigger to fire, which columns were modified in an UPDATE operation, and, in the case of a data definition language (DDL) trigger, an XML EventData structure (see Transact-SQL Reference) that describes the triggering operation.

当代码在SqlTriggerContext触发器SqlContext中通过属性运行时, 可以从类中获取的实例。 TriggerContextAn instance of SqlTriggerContext is available from the SqlContext class, when the code is running inside a trigger through the TriggerContext property.

属性

ColumnCount ColumnCount ColumnCount ColumnCount

获取绑定到触发器的数据表所包含的列的数目。Gets the number of columns contained by the data table bound to the trigger. 此属性是只读的。This property is read-only.

EventData EventData EventData EventData

获取特定于激发触发器的操作的事件数据。Gets the event data specific to the action that fired the trigger.

TriggerAction TriggerAction TriggerAction TriggerAction

指示激发触发器的操作。Indicates what action fired the trigger.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsUpdatedColumn(Int32) IsUpdatedColumn(Int32) IsUpdatedColumn(Int32) IsUpdatedColumn(Int32)

如果列受 INSERT 或 UPDATE 语句的影响,则返回 trueReturns true if a column was affected by an INSERT or UPDATE statement.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于