通过在 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功能的更多相关文章

  1. SQL Server CLR 使用 C# 自定义存储过程和触发器

    资源来源:https://www.cnblogs.com/Brambling/p/8016060.html SQL Server CLR 使用 C# 自定义存储过程和触发器   这一篇博客接着上一篇博 ...

  2. SQL Server 2014新功能PPT

        本篇文章是我在公司内部分享SQL Server 2014新功能的PPT,在本PPT中我详细描述了SQL Server除了BI方面的新功能,以及提供了大量的测试.希望对大家有帮助.     请点 ...

  3. SQL点滴7—使用SQL Server的attach功能出现错误及解决方法

    原文:SQL点滴7-使用SQL Server的attach功能出现错误及解决方法 今天用SQL Server 2008的attach功能附加一个数据库,出了点问题,提示的错误是: Unable to ...

  4. SQL Server 后续去除功能汇总

    原文:SQL Server 后续去除功能汇总 功能更新去除汇总 字段类型 在 Microsoft SQL Server 的未来版本中将删除 ntext.text 和 image 数据类型. 请避免在新 ...

  5. (原)SQL Server 系统提供功能的三个疑惑

    本文目录列表: 1.SQL Server系统提供的部分疑惑概述2.系统函数调用时DEFAULT代替可选参数使用不统一3.队列字段列message_enqueue_time记录的是UTC日期时间 4.@ ...

  6. SQL Server CLR 使用 C# 自定义函数

    一.简介 Microsoft SQL Server 2005之后,实现了对 Microsoft .NET Framework 的公共语言运行时(CLR)的集成.CLR 集成使得现在可以使用 .NET ...

  7. 【转】SQL SERVER CLR存储过程实现

    最近做一个项目,需要做一个SQL SERVER 2005的CLR的存储过程,研究了一下CLR的实现.为方便以后再使用,在这里总结一下我的实现流程,也供对CLR感兴趣但又不知道如何实现的朋友们做一下参考 ...

  8. SQL Server CLR全功略之一---CLR介绍和配置

    Microsoft SQL Server 现在具备与 Microsoft Windows .NET Framework 的公共语言运行时 (CLR) 组件集成的功能.CLR 为托管代码提供服务,例如跨 ...

  9. Sql Server 2016新功能之 Row-Level Security

    Sql Server 2016 有一个新功能叫 Row-Level Security ,大概意思是行版本的安全策略(原来我是个英语渣_(:з」∠)_) 直接上例子.这个功能相当通过对表添加一个函数作为 ...

随机推荐

  1. PAT乙级1043

    题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805280074743808 题解 这次再次体会到题意理解的正确 ...

  2. 部署ELK

    1.搭建客户机Nginx ls[root@nginx ~]# hostname nginx [root@nginx ~]# cat /etc/redhat-release CentOS release ...

  3. linear 工作流

    最近把 最后一张画ui的rt 从float换成srgb 并没有引起我预计会有的 alpha混合结果发生变化的事情 我想是因为 1.artists在ps里的工作流是线性空间 2.onchip memor ...

  4. hive数据仓库表设计之(矮宽表+高窄表)

    昨天面对某客户域做表关联的时候发现了. 有两张相同内容的主表.但是表的设计结构并不相同: (每个领域都有主表,每次往这个领域(库)添加新表的时候一般都会join 主表,从而有唯一的主键id) 这两个表 ...

  5. python内置函数(二)之filter,map,sorted

    filter filter()函数接收一个函数 f 和一个iterable的对象,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条 ...

  6. BZOJ 4802: 欧拉函数 (Pollard-Rho)

    开始一直T,原来是没有srand- CODE #include<bits/stdc++.h> using namespace std; typedef long long LL; vect ...

  7. 牛客寒假算法基础集训营2 【处女座与复读机】DP最小编辑距离【模板题】

    链接:https://ac.nowcoder.com/acm/contest/327/G来源:牛客网 一天,处女座在牛客算法群里发了一句“我好强啊”,引起无数的复读,可是处女座发现复读之后变成了“处女 ...

  8. 016_linuxC++之_多重继承

    (一)代码 #include <iostream> #include <string.h> #include <unistd.h> using namespace ...

  9. vue-cli3构建多页面应用

    创建一个项目hello-world vue create hello-worldcd hello-worldnpm run serve 在src目录下新建pages目录,在pages下新建页面 App ...

  10. HDU 2176 取(m堆)石子游戏 —— (Nim博弈)

    如果yes的话要输出所有情况,一开始觉得挺难,想了一下也没什么. 每堆的个数^一下,答案不是0就是先取者必胜,那么对必胜态显然至少存在一种可能性使得当前局势变成必败的.只要任意选取一堆,把这堆的数目变 ...