.NET:SqlDataReader.Close或.Dispose结果超时到期异常 [英] .NET: SqlDataReader.Close or .Dispose results in Timeout Expired exception
问题描述
当试图调用关闭或处置一个SqlDataReader我得到一个超时到期的异常。如果你有一个DbConnection到SQL Server,你可以自己再现它:
When trying to call Close or Dispose on an SqlDataReader i get a timeout expired exception. If you have a DbConnection to SQL Server, you can reproduce it yourself with:
String CRLF = "\r\n";
String sql =
"SELECT * " + CRLF +
"FROM (" + CRLF +
" SELECT (a.Number * 256) + b.Number AS Number" + CRLF +
" FROM master..spt_values a," + CRLF +
" master..spt_values b" + CRLF +
" WHERE a.Type = 'p'" + CRLF +
" AND b.Type = 'p') Numbers1" + CRLF +
" FULL OUTER JOIN (" + CRLF +
" SELECT (print("code sample");a.Number * 256) + b.Number AS Number" + CRLF +
" FROM master..spt_values a," + CRLF +
" master..spt_values b" + CRLF +
" WHERE a.Type = 'p'" + CRLF +
" AND b.Type = 'p') Numbers2" + CRLF +
" ON 1=1";
DbCommand cmd = connection.CreateCommand();
cmd.CommandText = sql;
DbDataReader rdr = cmd.ExecuteReader();
rdr.Close();
如果调用reader.Close()或reader.Dispose(),它将抛出一个System.Data .SqlClient.SqlException:
If you call reader.Close() or reader.Dispose() it will throw a System.Data.SqlClient.SqlException:
- ErrorCode:-2146232060(0x80131904)
- 讯息:
推荐答案
这是因为你刚刚打开了数据读取器,还没有完全迭代。您将需要.Cancel()您的DbCommand对象,然后再尝试关闭尚未完成的数据读取器(以及DbConnection)。当然,通过.Cancel() - 你的DbCommand,我不知道这个,但你可能会遇到一些其他异常。但你应该赶上它,如果发生。
it's because you have just opened the data reader and have not completely iterated through it yet. you will need to .Cancel() your DbCommand object before you attempt to close a data reader that hasn't completed yet (and the DbConnection as well). of course, by .Cancel()-ing your DbCommand, I'm not sure of this but you might encounter some other exception. but you should just catch it if it happens.
这篇关于.NET:SqlDataReader.Close或.Dispose结果超时到期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!