博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用Transact-SQL进行事务处理[示例]
阅读量:7239 次
发布时间:2019-06-29

本文共 2814 字,大约阅读时间需要 9 分钟。

如何利用Transact-SQL执行事务

下列存储过程说明了如何在Transact-SQL过程内执行事务的支金转移操作。

CREATE PROCEDURE MoneyTransfer@FromAccount char(20),@ToAccount char(20),@Amount moneyASBEGIN TRANSACTION-- PERFORM DEBIT OPERATIONUPDATE AccountsSET Balance = Balance - @AmountWHERE AccountNumber = @FromAccountIF @@RowCount = 0BEGINRAISERROR('Invalid From Account Number', 11, 1)GOTO ABORTENDDECLARE @Balance moneySELECT @Balance = Balance FROM ACCOUNTSWHERE AccountNumber = @FromAccountIF @BALANCE < 0BEGINRAISERROR('Insufficient funds', 11, 1)GOTO ABORTEND-- PERFORM CREDIT OPERATIONUPDATE AccountsSET Balance = Balance + @AmountWHERE AccountNumber = @ToAccountIF @@RowCount = 0BEGINRAISERROR('Invalid To Account Number', 11, 1)GOTO ABORTENDCOMMIT TRANSACTIONRETURN 0ABORT:ROLLBACK TRANSACTIONGO

该存储过程使用BEGIN TRANSACTION, COMMIT TRANSACTION,和ROLLBACK TRANSACTION状态手工控制事务。

如何编码事务性的.NET类

下述例子是三种服务性的NET类,它们配置或用于自动事务。每个类都带有Transaction属性,它的值将决定是否启动新事务流或者对象是否共享即时调用程序的数据流。这些元素一起工作来执行银行支金转移。Transfer类配置有RequiresNew事务属性,而Debit和Credit类配置有Required属性。这样,在运行的时候三个对象共享同一个事务。

using System;using System.EnterpriseServices;[Transaction(TransactionOption.RequiresNew)]public class Transfer : ServicedComponent{[AutoComplete]public void Transfer( string toAccount,string fromAccount, decimal amount ){try{// Perform the debit operationDebit debit = new Debit();debit.DebitAccount( fromAccount, amount );// Perform the credit operationCredit credit = new Credit();credit.CreditAccount( toAccount, amount );}catch( SqlException sqlex ){// Handle and log exception details// Wrap and propagate the exceptionthrow new TransferException( "Transfer Failure", sqlex );}}}[Transaction(TransactionOption.Required)]public class Credit : ServicedComponent{[AutoComplete]public void CreditAccount( string account, decimal amount ){SqlConnection conn = new SqlConnection("Server=(local); Integrated Security=SSPI"; database="SimpleBank");SqlCommand cmd = new SqlCommand("Credit", conn );cmd.CommandType = CommandType.StoredProcedure;cmd.Parameters.Add( new SqlParameter("@AccountNo", account) );cmd.Parameters.Add( new SqlParameter("@Amount", amount ));try{conn.Open();cmd.ExecuteNonQuery();}catch (SqlException sqlex){// Log exception details herethrow; // Propagate exception}}}[Transaction(TransactionOption.Required)]public class Debit : ServicedComponent{public void DebitAccount( string account, decimal amount ){SqlConnection conn = new SqlConnection("Server=(local); Integrated Security=SSPI"; database="SimpleBank");SqlCommand cmd = new SqlCommand("Debit", conn );cmd.CommandType = CommandType.StoredProcedure;cmd.Parameters.Add( new SqlParameter("@AccountNo", account) );cmd.Parameters.Add( new SqlParameter("@Amount", amount ));try{conn.Open();cmd.ExecuteNonQuery();}catch (SqlException sqlex){// Log exception details herethrow; // Propagate exception back to caller}}}

转载地址:http://iwgfm.baihongyu.com/

你可能感兴趣的文章
《用友ERP-U8完全使用详解》——第2章 安装用友ERP-U8(V8.72) 2.1 安装用友ERP-U8(V8.72)软件...
查看>>
海底机器人 OpenROV : 可以让普通人探索深海
查看>>
《Storm企业级应用:实战、运维和调优》——1.2 Storm是什么
查看>>
《Photoshop Lightroom5经典教程》目录—导读
查看>>
《LDA漫游指南》——2.7 总结
查看>>
《深入实践Spring Boot》一1.2 创建项目工程
查看>>
《流量的秘密 Google Analytics网站分析与商业实战》一导读
查看>>
《HTML5和CSS3快速参考》——导读
查看>>
科学家画出了完整的生命树
查看>>
《JavaScript设计模式》——1.5 真假对象
查看>>
Mining the Social Web
查看>>
《JavaScript入门经典(第6版)》——2.3 变量
查看>>
《Java遗传算法编程》—— 1.3 进化计算的历史
查看>>
2014年5个最流行前端框架对比
查看>>
使用Ruby amb解决说谎者谜题
查看>>
弗拉特利定律:Illumina如何缔造基因革命
查看>>
世界六大银行巨头重金押注哪些金融科技初创公司?
查看>>
还有两场,阿里聚安全在广州和上海等你
查看>>
《UML用户指南(第2版.修订版)》—第2章2.2节UML的概念模型
查看>>
《21天学通C++(第7版)》——17.6 问与答
查看>>