本文整理汇总了C#中Microsoft.SqlServer.Server.SqlTriggerContext.TriggerAction属性的典型用法代码示例。如果您正苦于以下问题:C# SqlTriggerContext.TriggerAction属性的具体用法?C# SqlTriggerContext.TriggerAction怎么用?C# SqlTriggerContext.TriggerAction使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类Microsoft.SqlServer.Server.SqlTriggerContext
的用法示例。
在下文中一共展示了SqlTriggerContext.TriggerAction属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TableAudit
[SqlTrigger(Name = @"TableAudit", Target = "[dbo].[Users]", Event = "FOR INSERT, DELETE")]
public static void TableAudit()
{
SqlCommand command = new SqlCommand();
SqlTriggerContext triggContext = SqlContext.TriggerContext;
SqlDataReader reader;
switch (triggContext.TriggerAction)
{
// Insert.
case TriggerAction.Insert:
using (SqlConnection connection
= 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();
}
break;
// Delete.
case TriggerAction.Delete:
using (SqlConnection connection
= 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)
{
while (reader.Read())
{
// Retrieve data from deleted rows.
}
reader.Close();
}
else
{
// No rows affected.
}
}
break;
}
}