c# – TransactionScope不回滚事务
发布时间:2020-12-15 17:49:00 所属栏目:百科 来源:网络整理
导读:这是我的交易范围源代码的当前体系结构.第三个插入引用.NET异常(不是SQL异常),而不是回滚两个先前的insert语句.我做错了什么? 编辑:我从insert2和insert3中删除了try / catch.我还从insert1 try / catch中删除了异常处理实用程序,并放置“throw ex”.它仍
这是我的交易范围源代码的当前体系结构.第三个插入引用.NET异常(不是SQL异常),而不是回滚两个先前的insert语句.我做错了什么?
编辑:我从insert2和insert3中删除了try / catch.我还从insert1 try / catch中删除了异常处理实用程序,并放置“throw ex”.它仍然不会回滚事务. 编辑2:我在Insert3方法中添加了try / catch,只是在catch语句中放了一个“throw”.它仍然不会回滚事务. 更新:根据我收到的反馈,“SqlHelper”类使用SqlConnection对象建立与数据库的连接,然后创建一个SqlCommand对象,将CommandType属性设置为“StoredProcedure”并调用SqlCommand的ExecuteNonQuery方法. 我也没有将Transaction Binding = Explicit Unbind添加到当前的连接字符串.我会补充说,在我的下一个测试. public void InsertStuff() { try { using(TransactionScope ts = new TransactionScope()) { //perform insert 1 using(SqlHelper sh = new SqlHelper()) { SqlParameter[] sp = { /* create parameters for first insert */ }; sh.Insert("MyInsert1",sp); } //perform insert 2 this.Insert2(); //perform insert 3 - breaks here!!!!! this.Insert3(); ts.Complete(); } } catch(Exception ex) { throw ex; } } public void Insert2() { //perform insert 2 using(SqlHelper sh = new SqlHelper()) { SqlParameter[] sp = { /* create parameters for second insert */ }; sh.Insert("MyInsert2",sp); } } public void Insert3() { //perform insert 3 using(SqlHelper sh = new SqlHelper()) { SqlParameter[] sp = { /*create parameters for third insert */ }; sh.Insert("MyInsert3",sp); } } 解决方法
我也遇到类似的问题.我的问题出现是因为我在SqlCommands中使用的SqlConnection在TransactionScope创建之前已经打开,因此它不会作为事务登录在TransactionScope中.
SqlHelper类是否可以重新使用在输入TransactionScope块之前打开的SqlConnection实例? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |