.NET:SqlDataReader.Close或.Dispose导致超时过期例外 [英] .NET: SqlDataReader.Close or .Dispose results in Timeout Expired exception

查看:492
本文介绍了.NET:SqlDataReader.Close或.Dispose导致超时过期例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图调用关闭或处置上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:

  • 在错误code:-2146232060(0x80131904)
  • 消息:超时过期之前完成操作或服务器没有响应超时时间已过。<​​/ LI>
  • ErrorCode: -2146232060 (0x80131904)
  • Message: "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

推荐答案

那是因为你刚刚打开的数据读取器并没有完全通过它迭代呢。您尝试关闭,目前尚未完成数据读取器(和的DbConnection以及)之前,你需要.Cancel()的DbCommand对象。当然,通过.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屋!

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