SqlTriggerContext Class
Assembly: System.Data (in system.data.dll)
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" in SQL Server Books Online) that describes the triggering operation.
An instance of SqlTriggerContext is available from the SqlContext class, when the code is running inside a trigger through the TriggerContext property.
The following example shows a SqlTriggerContext object being used to determine if an Insert action occurred. 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 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
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.