SQL SERVER CLR Trigger功能
通过在 Microsoft SQL Server 中托管 CLR(称为 CLR 集成),开发人员可以在托管代码中编写存储过程、触发器、用户定义函数、用户定义类型和用户定义聚合函数, 改变了以前只能通过T-SQL语言来实现这些功能的局面。因为托管代码在执行之前会编译为本机代码,所以,在有些方案中可以大大提高性能。
1. 编写C#代码,编译成.NET 3.5的dll
public class Program
{
[SqlTrigger(Name = @"UsersAudit", Target = "[dbo].[users]", Event = "FOR INSERT")]
public static void UsersAudit()
{
SqlContext.Pipe.Send("UsersAudit start");
// 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;
} SqlContext.Pipe.Send("UsersAudit end");
} [Microsoft.SqlServer.Server.SqlProcedure]
public static void HelloWorld(out string text)
{
SqlContext.Pipe.Send("Hello world!" + Environment.NewLine);
text = "Hello world!";
}
}
2. SMSS中选择“可编程性” -> "程序集"->“右键”-》“新建程序集”,将编译的dll注册
3.打开SMSS查询窗口,执行一下的sql语句将触发器和存储过程注册。注意程序集名称和命名空间名称的格式。
CREATE PROCEDURE hello
@i nchar(25) OUTPUT
AS
EXTERNAL NAME SqlTriggerContextTest.Program.HelloWorld
GO
-- if the HelloWorldProc class is inside a namespace (called MyNS),
-- the last line in the create procedure statement would be
-- EXTERNAL NAME helloworld.[MyNS.HelloWorldProc].HelloWorld CREATE TRIGGER UsersAudit
ON [dbo].[users]
AFTER INSERT, UPDATE
AS
EXTERNAL NAME SqlTriggerContextTest.Program.UsersAudit
GO
4. 可以执行sql语句查看效果

运行 select * from sys.assemblies 可以查看注册的程序集
如果dll中需要使用TCP功能,那么程序集必须签名。
参考:https://www.cnblogs.com/alexcodinglife/articles/5563148.html
SQL SERVER CLR Trigger功能的更多相关文章
- SQL Server CLR 使用 C# 自定义存储过程和触发器
资源来源:https://www.cnblogs.com/Brambling/p/8016060.html SQL Server CLR 使用 C# 自定义存储过程和触发器 这一篇博客接着上一篇博 ...
- SQL Server 2014新功能PPT
本篇文章是我在公司内部分享SQL Server 2014新功能的PPT,在本PPT中我详细描述了SQL Server除了BI方面的新功能,以及提供了大量的测试.希望对大家有帮助. 请点 ...
- SQL点滴7—使用SQL Server的attach功能出现错误及解决方法
原文:SQL点滴7-使用SQL Server的attach功能出现错误及解决方法 今天用SQL Server 2008的attach功能附加一个数据库,出了点问题,提示的错误是: Unable to ...
- SQL Server 后续去除功能汇总
原文:SQL Server 后续去除功能汇总 功能更新去除汇总 字段类型 在 Microsoft SQL Server 的未来版本中将删除 ntext.text 和 image 数据类型. 请避免在新 ...
- (原)SQL Server 系统提供功能的三个疑惑
本文目录列表: 1.SQL Server系统提供的部分疑惑概述2.系统函数调用时DEFAULT代替可选参数使用不统一3.队列字段列message_enqueue_time记录的是UTC日期时间 4.@ ...
- SQL Server CLR 使用 C# 自定义函数
一.简介 Microsoft SQL Server 2005之后,实现了对 Microsoft .NET Framework 的公共语言运行时(CLR)的集成.CLR 集成使得现在可以使用 .NET ...
- 【转】SQL SERVER CLR存储过程实现
最近做一个项目,需要做一个SQL SERVER 2005的CLR的存储过程,研究了一下CLR的实现.为方便以后再使用,在这里总结一下我的实现流程,也供对CLR感兴趣但又不知道如何实现的朋友们做一下参考 ...
- SQL Server CLR全功略之一---CLR介绍和配置
Microsoft SQL Server 现在具备与 Microsoft Windows .NET Framework 的公共语言运行时 (CLR) 组件集成的功能.CLR 为托管代码提供服务,例如跨 ...
- Sql Server 2016新功能之 Row-Level Security
Sql Server 2016 有一个新功能叫 Row-Level Security ,大概意思是行版本的安全策略(原来我是个英语渣_(:з」∠)_) 直接上例子.这个功能相当通过对表添加一个函数作为 ...
随机推荐
- flyio 的请求封装
1.安装flyio.js npm install flyio --save-dev 2.在util创建一个fly.js用于封装 import Vue from 'vue' var Fly=requir ...
- 1.打开windows中功能的快捷方式
1.打开组策略 命令:gpedit.msc 2.打开注册表 命令:regedit 3.快速打开本地安全组策略 命令:secpol.msc 4.打开服务 命令:services.msc 5.系统退域的时 ...
- Mapreduce案例之Pi值估算
题目: 这个程序的原理是这样的.假如有一个边长为1的正方形.以正方形的一个端点为圆心,以1为半径,画一个圆弧,于是在正方形内就有了一个直角扇形.在正方形里随机生成若干的点,则有些点是在扇形内,有些点是 ...
- 01_Deepin15 下搭建python开发环境
https://blog.csdn.net/iimpact/article/details/90239193 https://github.com/josonle/Coding-Now#Linux系统 ...
- sb 的长度 和 文件大小
StringBuilder sb = new StringBuilder(); ;i<;i++) //1 0000 0000 1亿项 { sb.AppendFormat("{0}, ...
- 洛谷【P2257】 YY的GCD
出处:http://www.cnblogs.com/peng-ym/p/8652288.html ( 直接去出处那看就好了 ) 题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求 ...
- Java进阶知识11 Hibernate多对多单向关联(Annotation+XML实现)
1.Annotation 注解版 1.1.应用场景(Student-Teacher):当学生知道有哪些老师教,但是老师不知道自己教哪些学生时,可用单向关联 1.2.创建Teacher类和Student ...
- 【线性代数】2-6:三角矩阵( $A=LU$ and $A=LDU$ )
title: [线性代数]2-6:三角矩阵( A=LUA=LUA=LU and A=LDUA=LDUA=LDU ) toc: true categories: Mathematic Linear Al ...
- kubeadm进行K8S集群部署
环境说明: 节点类型 主机名 管理ip master master 192.168.2.10 nodes node1 192.168.2.1 ...
- 在docker 安装gitlab
一.Centos 7 上安装 官方文档:https://docs.docker.com/install/linux/docker-ce/centos/ 1.安装环境 yum install ...