我有这个代码:
// this is managed elsewhere
SqlConnection connection =...
connection.Open();
// this is one block of code, separate from the above
using( var transaction = connection.BeginTransaction() ) {
using( var command = connection.CreateCommand() ) {
command.Transaction = transaction;
command.CommandText = ...
using( var reader = command.ExecuteReader() ) {
if( reader.HasRows ) {
if( reader.Read() ) {
//get data from the reader
}
}
}
}
而且这段代码大部分时间都运行得很好。然而有时 - 很少 - 检索会HasRows
产生以下异常:
Invalid attempt to call HasRows when reader is closed.
System.InvalidOperationException
at System.Data.SqlClient.SqlDataReader.get_HasRows()
// my code calling HasRows listed here
我有 99.5% 的把握在那一刻连接是打开的。我的代码HasRows
在从读者那里阅读之前使用,就像 MSDN 建议的那样。
该异常的原因可能是什么?