CLR触发器实战演练
创始人
2024-06-07 13:01:19
0

CLR触发器的原理比较简单,但是实现起来要花一番功夫,笔者总结了以下经验,还有一个异常的实例。

CLR触发器实现代码:

  1. [Microsoft.SqlServer.Server.SqlTrigger(Name = "Trigger1", Target = 
  2. "ERP_STOCKYaoHuoDingDan", Event = "FOR INSERT")]  
  3. public static void DingDanIDSameGongYingShangGUIDMustSame()  
  4. {  
  5.   using (SqlConnection connection = new SqlConnection
  6. (@"context connection=true"))  
  7.   {  
  8.     connection.Open();  
  9.     SqlCommand command = new SqlCommand(@"SELECT COUNT(A.DingDanID)
  10.  FROM ERP_STOCKYaoHuoDingDan AS
  11.  A,INSERTED AS B WHERE A.DingDanID=B.DingDanID AND
  12.  A.GongYingShangGUID<>B.GongYingShangGUID", connection);  
  13.     int i=(int)command.ExecuteScalar();  
  14.      if (i>0)  
  15.    {  
  16.     try  
  17.      {  
  18.       //如果要插入的记录不合法,则回滚.   
  19.        Transaction trans = Transaction.Current;  
  20.        trans.Rollback();  
  21.      }  
  22.      catch (SqlException ex)  
  23.        {  
  24.               connection.Close(); 
  25.        }  
  26. }
  27. }
  28. }
  29.  

当在CLR触发器内部调用 Transaction.Rollback 方法时,将引发异常并显示不明确的错误消息,必须在try/catch 块中包装此方法或命令。您会看到如下错误消息:

Msg 6549, Level 16, State 1, Procedure trig_InsertValidator, Line 0 A .NET Framework error occurred during execution of user defined routine or aggregate 'trig_InsertValidator': System.Data.SqlClient.SqlException: Transaction is not allowed to roll back inside a user defined routine, trigger or aggregate because the transaction is not started in that CLR level. Change application logic to enforce strict transaction nesting… User transaction, if any, will be rolled back.

此异常是预期行为,需要 try/catch 块才能继续执行代码。当完成执行CLR触发器代码时,将引发另一个异常。

Msg 3991, Level 16, State 1, Procedure trig_InsertValidator, Line 1 The context transaction which was active before entering user defined routine, trigger or aggregate "trig_InsertValidator" has been ended inside of it, which is not allowed. Change application logic to enforce strict transaction nesting. The statement has been terminated.
此异常也是预期行为。

调用该CLR触发器的例子

尽管引发了两个异常,仍可以回滚事务,并且更改不会提交到表中。

  1. try  
  2. {  
  3.  
  4.     //用到此触发器的方法  
  5. }  
  6. catch (SqlException ex)  
  7. {  
  8.    if (ex.Number == 3991)  
  9.    {  
  10.       LabelInfor.Text = "同一张订单必须是同一家供应商。";  
  11.    }  
  12. }  
  13. catch (Exception ex)  
  14. {  
  15.  ......  

以上就是对CLR触发器的实战演练。

【编辑推荐】

  1. 浅析C#数组操作方法
  2. C#数组操作全面分析
  3. C#数组和串操作经验总结
  4. 为你解疑:CLR是什么?
  5. 分析与对比CLR Via C#静态构造函数的性能

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...