共用方式為


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) 觸發程式的情況下,XML EventData 結構 (請參閱描述觸發作業的 Transact-SQL 參考) 。

當程式碼透過 屬性在 SqlContext 觸發程式內執行時,可從 類別取得 的 TriggerContext 實例 SqlTriggerContext

屬性

ColumnCount

取得繫結至觸發程序之資料表格中包含的資料行數目。 這個屬性是唯讀的。

EventData

取得引發觸發程序之動作特定的事件資料。

TriggerAction

指出何種動作引發了觸發程序。

方法

IsUpdatedColumn(Int32)

如果 INSERT 或 UPDATE 陳述式 (Statement) 影響資料行,則會傳回 true

適用於