加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – SqlCommand.Cancel()导致性能提升?

发布时间:2020-12-15 06:20:33 所属栏目:百科 来源:网络整理
导读:我已经看到这个代码中出现了几个地方,从来没有一个解释,只是上面的一个隐秘的评论(声明和执行包括一个上下文的想法,这只是运行一个SqlCommand的标准过程): //SqlCommand cmd = new SqlCommand();//cmd.ExecuteReader();//Read off the results//Cancel the
我已经看到这个代码中出现了几个地方,从来没有一个解释,只是上面的一个隐秘的评论(声明和执行包括一个上下文的想法,这只是运行一个SqlCommand的标准过程):
//SqlCommand cmd = new SqlCommand();
//cmd.ExecuteReader();
//Read off the results

//Cancel the command. This improves query time.
cmd.Cancel ();

基本上,完成查询后,它会返回并取消它,声称有一些性能提升.我想你可能会得到一些回忆,当它去释放XmlReader,但通常它将要超出范围.

我以前从来没有打过它,但终于在我正在审查的代码中显示出来.在代码中运行它后,取消SqlCommand实际上会加快速度,还是这只是一些奇怪的程序迷信?

解决方法

根据 MSDN,这是正确的.

The Close method fills in the values
for output parameters,return values
and RecordsAffected,increasing the
time that it takes to close a
SqlDataReader that was used to process
a large or complex query. When the
return values and the number of
records affected by a query are not
significant,the time that it takes to
close the SqlDataReader can be reduced
by calling the Cancel method of the
associated SqlCommand object before
calling the Close method.

奇怪的!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读