SqlTriggerContext Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menyediakan informasi kontekstual tentang pemicu yang diaktifkan.
public ref class SqlTriggerContext sealed
public sealed class SqlTriggerContext
type SqlTriggerContext = class
Public NotInheritable Class SqlTriggerContext
- Warisan
-
SqlTriggerContext
Contoh
Contoh berikut menunjukkan objek yang SqlTriggerContext digunakan untuk menentukan apakah tindakan Insert terjadi. Jika baris disisipkan ke dalam user
tabel, nama pengguna dan nama asli diambil dari baris yang disisipkan lalu ditambahkan ke tabel 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
Keterangan
Informasi kontekstual yang diberikan mencakup jenis tindakan yang menyebabkan pemicu diaktifkan, kolom mana yang dimodifikasi dalam operasi UPDATE, dan, dalam kasus pemicu bahasa definisi data (DDL), struktur Xml EventData (lihat Referensi Transact-SQL) yang menjelaskan operasi pemicu.
Instans SqlTriggerContext tersedia dari SqlContext kelas , ketika kode berjalan di dalam pemicu melalui TriggerContext properti .
Properti
ColumnCount |
Mendapatkan jumlah kolom yang dimuat oleh tabel data yang terikat ke pemicu. Properti ini bersifat hanya baca. |
EventData |
Mendapatkan data peristiwa khusus untuk tindakan yang mengaktifkan pemicu. |
TriggerAction |
Menunjukkan tindakan apa yang mengaktifkan pemicu. |
Metode
IsUpdatedColumn(Int32) |
Mengembalikan |