使用.Net2.0的事务

在1.1框架时困扰我很久的事件调用处理终于在2.0的时候有了很好的解决方案.
1.1的时候因为企业事务要涉及COM+,而我对COM+又不懂,所以一直没有偿试它,而是采用ADO.Net的本地级事务.ADO.Net的事件有两个弱点:(1)不能实现分布式事务;(2)不能够很好的处理事务嵌套.
在2.0框架下,可以使用System.Transactions这个命名空间下的一些类,其中System.Transactions.TransactionScope,实现了隐式的事务处理方式,可以判断事务的类型来使用本地级事务或者是分布式事务.我的测试代码如下:

None.gif              try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
using (System.Data.SqlClient.SqlConnection sql1 = new System.Data.SqlClient.SqlConnection(@"Data Source=LYSERVER\LYSERVER;Initial Catalog=pubs;Persist Security Info=True;User ID=sa"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sql1.Open();
InBlock.gif                        System.Data.SqlClient.SqlCommand cmd 
= new System.Data.SqlClient.SqlCommand();
InBlock.gif                        cmd.Connection 
= sql1;
InBlock.gif                        cmd.CommandText 
= "INSERT INTO stores(stor_id,stor_name,stor_address,city,state,zip) values('8888','测试分布式事务','沈阳市三好街','沈阳','LY','111')";
InBlock.gif                        cmd.ExecuteNonQuery();
InBlock.gif                        sql1.Close();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if (MessageBox.Show("您要中断对本地数据库的访问吗?""", MessageBoxButtons.YesNo) == DialogResult.Yes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
throw new Exception("用户中断,自动回滚全部事务!");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
using (System.Data.SqlClient.SqlConnection sql1 = new System.Data.SqlClient.SqlConnection(@"Data Source=wu;Initial Catalog=pubs;Integrated Security=True"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sql1.Open();
InBlock.gif                        System.Data.SqlClient.SqlCommand cmd 
= new System.Data.SqlClient.SqlCommand();
InBlock.gif                        cmd.Connection 
= sql1;
InBlock.gif                        cmd.CommandText 
= "INSERT INTO stores(stor_id,stor_name,stor_address,city,state,zip) values('8888','测试分布式事务','沈阳市三好街','沈阳','LY','111')";
InBlock.gif                        cmd.ExecuteNonQuery();
InBlock.gif                        sql1.Close();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    scope.Complete();
InBlock.gif                    MessageBox.Show(
"commited");
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

None.gif            
catch  (Exception ex)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                MessageBox.Show(ex.Message);
ExpandedBlockEnd.gif            }
注意:在使用System.Transactions时,VS2005并没有引用其相应的Dll,需要手动的引用System.Transaction.dll.

转载于:https://www.cnblogs.com/fxwdl/archive/2007/03/14/674607.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值