编辑:我从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实例?
总结以上是内存溢出为你收集整理的c# – TransactionScope不回滚事务全部内容,希望文章能够帮你解决c# – TransactionScope不回滚事务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)