-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsource.vb
More file actions
121 lines (83 loc) · 3.35 KB
/
Copy pathsource.vb
File metadata and controls
121 lines (83 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Option Explicit
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
'The Partial modifier is only required on one class definition per project.
Partial Public Class CLRTriggers
' <Snippet1>
<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
'</Snippet1>
'<Snippet2>
<SqlTrigger(Name:="TableAudit", Target:="[dbo].[Users]", Event:="FOR INSERT, DELETE")> _
Public Shared Sub TableAudit()
Dim command As SqlCommand
Dim triggContext As Microsoft.SqlServer.Server.SqlTriggerContext
Dim reader As SqlDataReader
triggContext = SqlContext.TriggerContext
Select Case triggContext.TriggerAction
' Insert.
Case TriggerAction.Insert
Using connection As New SqlConnection("context connection=true")
' Open the context connection.
connection.Open()
' Get the inserted row.
command = New SqlCommand("SELECT * FROM INSERTED;", connection)
reader = command.ExecuteReader()
reader.Read()
' Retrieve data from inserted row.
reader.Close()
End Using
' Delete.
Case TriggerAction.Delete
Using connection As New SqlConnection("context connection=true")
' Open the context connection.
connection.Open()
' Get the deleted rows.
command = New SqlCommand("SELECT * FROM DELETED;", connection)
reader = command.ExecuteReader()
If reader.HasRows Then
While reader.Read()
' Retrieve data from deleted rows
End While
reader.Close()
Else
' No rows affected.
End If
End Using
End Select
End Sub
'</Snippet2>
End Class