偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

分析概括CLR觸發(fā)器的例子

開發(fā) 后端
文章主要介紹了創(chuàng)建CLR對(duì)象三大步1.在解決方案中添加新的C#SQL數(shù)據(jù)庫(kù)項(xiàng)目命名為“ServiceClient”2為你的目標(biāo)數(shù)據(jù)庫(kù)選擇或添加注釋 3.為創(chuàng)建的服務(wù)添加聲明。

CLR有很多值得學(xué)習(xí)的地方,這里我們主要介紹創(chuàng)建CLR對(duì)象,包括介紹CLR觸發(fā)器等方面,希望你可以從中學(xué)到東西。

一 創(chuàng)建CLR對(duì)象

1.在解決方案中添加新的C#SQL數(shù)據(jù)庫(kù)項(xiàng)目,命名為“ServiceClient”
2.為你的目標(biāo)數(shù)據(jù)庫(kù)選擇或添加注釋。(如果未被提示:右擊ServiceClient項(xiàng)目,選擇屬性,數(shù)據(jù)庫(kù),瀏覽并選擇你的連接)
3.為創(chuàng)建的服務(wù)添加聲明
1)在解決方案瀏覽器中右擊“service”項(xiàng)目,選擇“調(diào)試”>“Start New Instance”
2)服務(wù)運(yùn)行:右擊“ServiceClient”項(xiàng)目,選擇“Add Service Reference”
3)在“Service URI”中輸入:http://localhost:8000/services
4)點(diǎn)擊“OK”

二 CLR觸發(fā)器案例

  1. [Microsoft.SqlServer.Server.SqlTrigger(Name="Trigger1",Target="ERP_STOCKYaoHuoDingDan",Event="FORINSERT")]  
  2. publicstaticvoidDingDanIDSameGongYingShangGUIDMustSame()  
  3. {  
  4. using(SqlConnectionconnection=newSqlConnection(@"contextconnection=true"))  
  5. {  
  6. connection.Open();  
  7. SqlCommandcommand=newSqlCommand(@"SELECTCOUNT(A.DingDanID)FROMERP_STOCKYaoHuoDingDanASA,INSERTEDASBWHEREA.DingDanID=B.DingDanIDANDA.GongYingShangGUID<>B.GongYingShangGUID",connection);  
  8. inti=(int)command.ExecuteScalar();  
  9. if(i>0)  
  10. {  
  11. try  
  12. {  
  13. //如果要插入的記錄不合法,則回滾.  
  14. TransactionTransactiontrans=Transaction.Current;  
  15. trans.Rollback();  
  16. }  
  17. catch(SqlExceptionex)  

當(dāng)在觸發(fā)器內(nèi)部調(diào)用Transaction.Rollback方法時(shí),將引發(fā)異常并顯示不明確的錯(cuò)誤消息,必須在try/catch塊中包裝此方法或命令。您會(huì)看到如下錯(cuò)誤消息:

  1. Msg6549,Level16,State1,Proceduretrig_InsertValidator,Line0A.NETFrameworkerroroccurredduringexecutionofuserdefinedroutineoraggregate  
  2. 'trig_InsertValidator':System.Data.SqlClient.SqlException:Transactionisnotallowedtorollbackinsideauserdefinedroutine,  
  3. triggeroraggregatebecausethetransactionisnotstartedinthatCLRlevel.Changeapplicationlogictoenforcestricttransactionnesting…Usertransaction,ifany,willberolledback.  
  4. 此異常是預(yù)期行為,需要try/catch塊才能繼續(xù)執(zhí)行代碼。當(dāng)完成執(zhí)行觸發(fā)器代碼時(shí),將引發(fā)另一個(gè)異常。  
  5. Msg3991,Level16,State1,Proceduretrig_InsertValidator,Line1Thecontexttransactionwhichwasactivebeforeenteringuserdefinedroutine,  
  6. triggeroraggregate"trig_InsertValidator"hasbeenendedinsideofit,whichisnotallowed.Changeapplicationlogictoenforcestricttransactionnesting.  
  7. Thestatementhasbeenterminated.此異常也是預(yù)期行為。  
  8. }  
  9. }  
  10. connection.Close();  
  11. }  

調(diào)用CLR觸發(fā)器的例子

盡管引發(fā)了兩個(gè)異常,仍可以回滾事務(wù),并且更改不會(huì)提交到表中。

  1. try  
  2. {  
  3. //用到此觸發(fā)器的方法  
  4. }  
  5. catch(SqlExceptionex)  
  6. {  
  7. if(ex.Number==3991)  
  8. {  
  9. LabelInfor.Text="同一張訂單必須是同一家供應(yīng)商。";  
  10. }  
  11. }  
  12. catch(Exceptionex)  
  13. {  
  14. }  

【編輯推薦】

  1. 使用CLR存儲(chǔ)過(guò)程來(lái)傳回訊息實(shí)戰(zhàn)案例
  2. 經(jīng)典案例之創(chuàng)建CLR存儲(chǔ)過(guò)程
  3. 概括Visual Studio CLR調(diào)試器兩種方法
  4. Visual Studio中SQL SERVER CLR代碼調(diào)試工具使用講解
  5. CLR函數(shù)壓縮NTEXT類型字段實(shí)例講解
責(zé)任編輯:田樹 來(lái)源: 博客
相關(guān)推薦

2009-09-18 14:31:33

CLR觸發(fā)器

2011-05-20 14:06:25

Oracle觸發(fā)器

2010-05-19 09:40:05

MySQL觸發(fā)器

2011-04-14 13:54:22

Oracle觸發(fā)器

2011-05-19 14:29:49

Oracle觸發(fā)器語(yǔ)法

2010-10-12 10:04:15

MySQL觸發(fā)器

2010-05-31 18:06:07

MySQL 觸發(fā)器

2009-10-22 13:47:12

CLR調(diào)試器

2011-03-28 10:05:57

sql觸發(fā)器代碼

2009-04-07 13:56:03

SQL Server觸發(fā)器實(shí)例

2010-05-18 15:36:44

MySQL觸發(fā)器

2010-10-11 14:52:43

Mysql觸發(fā)器

2009-04-26 22:27:54

觸發(fā)器密碼修改數(shù)據(jù)庫(kù)

2011-03-03 09:30:24

downmoonsql登錄觸發(fā)器

2009-11-18 13:15:06

Oracle觸發(fā)器

2021-07-30 10:33:57

MySQL觸發(fā)器數(shù)據(jù)

2010-10-12 10:24:58

mysql觸發(fā)器

2010-05-18 15:58:39

MySQL觸發(fā)器

2010-10-20 14:34:48

SQL Server觸

2010-10-12 09:41:26

mysql觸發(fā)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)