1. using System;
  2. using System.Data.SqlClient;
  3. using System.Transactions;
  4.  
  5. namespace SomeDBTransaction
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string con1 = "SERVER=.; DATABASE=db1; UID=sa; PWD=llh";
  12. string con2 = "SERVER=.; DATABASE=db2; UID=sa; PWD=llh";
  13. string sqlStr1 = "U_t1";
  14. string sqlStr2 = "U_t1";
  15.  
  16. int resu=CreateTransactionScope(con1, con2, sqlStr1, sqlStr2);
  17. Console.WriteLine("受影响的行数:"+resu);
  18.  
  19. Console.Read();
  20. }
  21.  
  22. // This function takes arguments for 2 connection strings and commands to create a transaction
  23. // involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the
  24. // transaction is rolled back. To test this code, you can connect to two different databases
  25. // on the same server by altering the connection string, or to another 3rd party RDBMS by
  26. // altering the code in the connection2 code block.
  27. static public int CreateTransactionScope(
  28. string connectString1, string connectString2,
  29. string commandText1, string commandText2)
  30. {
  31. // Initialize the return value to zero and create a StringWriter to display results.
  32. int returnValue = ;
  33. System.IO.StringWriter writer = new System.IO.StringWriter();
  34.  
  35. try
  36. {
  37. // Create the TransactionScope to execute the commands, guaranteeing
  38. // that both commands can commit or roll back as a single unit of work.
  39. using (TransactionScope scope = new TransactionScope())
  40. {
  41. using (SqlConnection connection1 = new SqlConnection(connectString1))
  42. {
  43. // Opening the connection automatically enlists it in the
  44. // TransactionScope as a lightweight transaction.
  45. connection1.Open();
  46.  
  47. // Create the SqlCommand object and execute the first command.
  48. SqlCommand command1 = new SqlCommand(commandText1, connection1);
  49. command1.CommandType = System.Data.CommandType.StoredProcedure;
  50. returnValue = command1.ExecuteNonQuery();
  51. writer.WriteLine("Rows to be affected by command1: {0}", returnValue);
  52.  
  53. // If you get here, this means that command1 succeeded. By nesting
  54. // the using block for connection2 inside that of connection1, you
  55. // conserve server and network resources as connection2 is opened
  56. // only when there is a chance that the transaction can commit.
  57. using (SqlConnection connection2 = new SqlConnection(connectString2))
  58. {
  59. // The transaction is escalated to a full distributed
  60. // transaction when connection2 is opened.
  61. connection2.Open();
  62.  
  63. // Execute the second command in the second database.
  64. returnValue = ;
  65. SqlCommand command2 = new SqlCommand(commandText2, connection2);
  66. command1.CommandType = System.Data.CommandType.StoredProcedure;
  67. returnValue = command2.ExecuteNonQuery();
  68. writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
  69. }
  70. }
  71.  
  72. // The Complete method commits the transaction. If an exception has been thrown,
  73. // Complete is not called and the transaction is rolled back.
  74. scope.Complete();
  75.  
  76. }
  77.  
  78. }
  79. catch (TransactionAbortedException ex)
  80. {
  81. writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
  82. }
  83. catch (ApplicationException ex)
  84. {
  85. writer.WriteLine("ApplicationException Message: {0}", ex.Message);
  86. }
  87. catch (Exception ex)
  88. {
  89. writer.WriteLine("ERROR: {0}",ex.Message);
  90. }
  91. // Display messages.
  92. Console.WriteLine(writer.ToString());
  93.  
  94. return returnValue;
  95. }
  96. }
  97.  
  98. }

CODE

代码很简单啦 ~~,就多加了一个using而已

C#轻量级企业事务 - TransactionScope的更多相关文章

  1. Cassandra如何利用线性一致性来实现轻量级的事务

    分布式数据库会面临着一个独特的挑战,就是数据必须要严格的按照读,写顺序执行.如创建用户,转账,两个潜在的写操作竞态条件必须要确保一个写操作必须在另外一个之前发生.在Cassandra中,使用Paxos ...

  2. 分布式事务TransactionScope

    分布式事务TransactionScope 以下是分布式事务的所有情况的例子了,包含了事务套事务,事务套存储过程事务,经过测试,TransactionScope对于分布式事务的各种情况支持的很好. 使 ...

  3. 探索逻辑事务 TransactionScope

    一.什么是TransactionScope? TransactionScope即范围事务(类似数据库中的事务),保证事务声明范围内的一切数据修改操作状态一致性,要么全部成功,要么全部失败回滚. MSD ...

  4. 代码块事务—TransactionScope

    今天上班遇到这样的业务:将删除的用户信息记录到记录表,再删除用户表中的信息. 可以说是不幸也可以说是幸运的. 在以往遇到这样的业务,我会考虑到各种出现异常或者失败的情况.在删除一张表数据失败的情况,对 ...

  5. 【NET Core】事务TransactionScope

    .NET FrameWork时期: TransactionScope是FCL System.Transactions命名空间下的分布式事务组件,它默认为本地事务,当系统有需要时可以自动提升为分布式事务 ...

  6. 分布式事务TransactionScope所导致几个坑

    记录一下,个人见解,欢迎指正 错误: 1.该伙伴事务管理器已经禁止了它对远程/网络事务的支持. (异常来自 HRESULT:0x8004D025)2.事务已被隐式或显式提交,或已终止3.此操作对该事务 ...

  7. 轻量级企业私有云 JimV 分享

    当前云市场分析 云分两种,公有云.私有云.目前市面上的云产品,对于中小规模的企业来讲,痛点有如下几点: 私有云: 1.VMware ESXi 类: a) 授权费用昂贵: b) 创建虚拟机费时费力: 2 ...

  8. C# TransactionScope 事务类

    微软自带的TransactionScope(.Net Framework 2之后)是个好东东,提供的功能也很强大. 首先说说TransactionScope是什么,并能为我们做什么事情.其实看Tran ...

  9. 事务使用中如何避免误用分布式事务(System.Transactions.TransactionScope)

    1:本地事务DbTransaction和分布式事务TransactionScope的区别: 1.1:System.Data.Common.DbTransaction: 本地事务:这个没什么好说了,就是 ...

随机推荐

  1. Docker基础技术:DeviceMapper

    在上一篇介绍AUFS的文章中,大家可以看到,Docker的分层镜像是怎么通过UnionFS这种文件系统做到的,但是,因为Docker首选的AUFS并不在Linux的内核主干里,所以,对于非Ubuntu ...

  2. URAL 1233 Amusing Numbers 好题

    参照了nocow上的解法,照搬过来…… 易知一个数X在数列中在另一个数Y前,当且仅当X前缀小于Y或前缀相等X短,那么我们分布考虑,比如对于数48561: 5位上:10000~48560; 4位上:10 ...

  3. EasyUi datagrid 表格分页例子

    1.首先引入 easyui的 css 和 js 文件 2.前台 需要写的js //源数据 function Async(action,args,callback){  $.ajax({  url: a ...

  4. eclipse中maven项目部署到tomcat

    其实maven项目部署到tomcat的方式很多,我从一开始的打war包到tomcat/webapps目录,到使用tomcat-maven插件,到直接使用servers部署,一路来走过很多弯路. 下面就 ...

  5. OkHttp使用进阶(译自OkHttp官方教程)

    没有使用过OkHttp的,可以先看OkHttp使用介绍 英文版原版地址 Recipes · square/okhttp Wiki 同步get 下载一个文件,打印他的响应头,以string形式打印响应体 ...

  6. 【分享】IT产业中的三大定理(二) —— 安迪&比尔定理 (Andy and Bill's Law)

    摩尔定理给所有的计算机消费者带来一个希望,如果我今天嫌计算机太贵买不起,那么我等十八个月就可以用一半的价钱来买.要真是这样简单的话,计算机的销售量就上不去了.需要买计算机的人会多等几个月,已经有计算机 ...

  7. poj 1017 Packets 裸贪心

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43189   Accepted: 14550 Descrip ...

  8. MVC用户登陆验证及权限检查(Form认证)

    1.配置Web.conf,使用Form认证方式   <system.web>     <authentication mode="None" />      ...

  9. HDU 4658 Integer Partition(整数拆分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4658 题意:给出n.k.求n的拆分方案数.要求拆分中每个数不超过k. i64 f[N]; void i ...

  10. 总结Selenium自动化测试方法(五)自动化测试框架

    五.自动化测试框架 1.单元测试框架unittest class loginTests(unittest.TestCase): ①开始的初始化部分 @classmethod def setUpClas ...