将数据读取器与 sqltransactions 结合使用 [英] Using datareaders with sqltransactions

查看:38
本文介绍了将数据读取器与 sqltransactions 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中使用 SqlTransactions 用于回滚目的.在事务中,我得到了多个要执行的语句,其中可能包括选择插入和更新.所有这些语句都在 sqltransaction 的范围内.一切正常,仅针对一个问题.我正在使用 datareaders 进行选择语句.而这些阅读器一旦使用就关闭了.这会迫使连接丢失,一切都失败了.有没有人有关于我是否可以在 sqltransaction 中使用 datareader 的解决方案??

I am using SqlTransactions in my code for roll back purpose. Within the transaction I got multiple statements to be executed with may include selects inserts and updates. All these statements are within the scope of the sqltransaction. Everything works fine just for one problem. I am using datareaders for select statements . And these readers are closed once they are used. This forces the connection to be lost and every thing fails. Does any one have a solution on whether I can use datareaders within a sqltransaction??

推荐答案

只有在调用 ExecuteReader 时设置了 CommandBehavior.CloseConnection 选项,DataReader 才会关闭连接.

A DataReader will only close the connection if the CommandBehavior.CloseConnection option was set when calling ExecuteReader.

如果您避免设置此选项应该没问题.

You should be OK if you avoid setting this option.

这篇关于将数据读取器与 sqltransactions 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆