通过在 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. bug-- java.lang.RuntimeException: Type “Klass*"

      使用jinfo查看jvm进程id为27523的信息   [java@xftest0 ~]$ jinfo 27523 Attaching to process ID 27523, please wa ...

  2. 基于ElementUI封装可复用的表格组件

    <template> <section class="ces-table-page"> <!-- 表格操作按钮 --> <section ...

  3. 爬当当网上python书籍的图片

    1.分析网页代码,获取图片下载连接:http://img3m4.ddimg.cn/20/11/23473514-1_b_5.jpg 2. python实现代码 import os import re ...

  4. IDEA Java

    目录 1 配置 2 常用快捷键 3 安装插件 4 使用Maven创建web项目 5 使用Maven导入依赖 6 Maven创建项目后缺少文件夹 7 Tomcat LocalHost Log消失 8 E ...

  5. PHP mysqli_fetch_field() 函数

    返回结果集中下一字段(列),然后输出每个字段名称.表格和最大长度: <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con=mysqli_connect ...

  6. Lua unpack函数用法

    unpack,接受一个table做个参数,然后按照下标返回数组的所有元素 unpack lua 版本 <= 5.1 local t = {nil , 3} retunrn unpack(t) / ...

  7. HDU 3689 Infinite monkey theorem ——(自动机+DP)

    这题由于是一个单词,其实直接kmp+dp也无妨.建立自动机当然也是可以的.设dp[i][j]表示匹配到第i个字母的时候,在单词中处于第j个位置的概率,因此最终的答案是dp[0~m][len],m是输入 ...

  8. CF1204A

    CF1204A. BowWow and the Timetable 题意: 给你一个2进制数,求这个2进制数在10进制中的 $ 4^i $ 的个数. 解法: 其实就是 $ \ulcorner_{\lo ...

  9. java将PDF的前几页拆出来组成新pdf

    /** * 截取pdfFile的第from页至第end页,组成一个新的文件名 * @param pdfFile 需要分割的PDF * @param savepath 新PDF * @param fro ...

  10. mysql delete别名

    有一个表的数据比较大,然后需要进行关联删除,删除的时候发现如下SQL报错:ELETE FROM test.test1 a WHERE EXISTS (SELECT 1 FROM test.test2 ...