问题描述
我在我的代码中使用 SqlTransaction
s 用于回滚目的.在事务中,我得到了多个要执行的语句,其中可能包括选择插入和更新.所有这些语句都在 sqltransaction
的范围内.一切正常,仅针对一个问题.我正在使用 datareader
s 进行选择语句.而这些阅读器一旦使用就关闭了.这会迫使连接丢失,一切都失败了.有没有人有关于我是否可以在 sqltransaction
中使用 datareader
的解决方案??
I am using SqlTransaction
s 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 datareader
s 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 datareader
s 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 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!