简体   繁体   English

ServerConnection.Cancel方法

[英]ServerConnection.Cancel method

The SqlCommand class has a Cancel method that, according to the documentation, allows one to cancel a command. SqlCommand类有一个Cancel方法,根据文档,它允许取消命令。

I'm looking for an equivalent method for commands executed through the smo Server class . 我正在寻找通过smo Server执行的命令的等效方法。 The ServerConnection class (connectioncontext field of the server class) has a Cancel method but the documentation is not clear on what it does. ServerConnection类(服务器类的connectioncontext字段) 具有Cancel方法,但文档不清楚它的作用。

The SeverConnection.Cancel() Method works similarly to the SqlCommand.Cancel() method and the Documentation states: SeverConnection.Cancel()方法与SqlCommand.Cancel()方法的工作方式类似文档指出:

If there is nothing to cancel, nothing occurs. 如果没有什么可以取消,则不会发生任何事情 However, if there is a command in process, and the attempt to cancel fails, no exception is generated. 但是,如果正在处理命令,并且尝试取消失败,则不会生成异常。

In some, rare, cases, if you call ExecuteReader then call Close (implicitily or explicitly) before calling Cancel, and then call Cancel, the cancel command will not be sent to SQL Server and the result set can continue to stream after you call Close. 在某些罕见情况下,如果调用ExecuteReader然后在调用Cancel之前调用Close(隐式或显式),然后调用Cancel,则取消命令将不会发送到SQL Server,并且结果集可以在调用Close后继续流式传输。 To avoid this, make sure that you call Cancel before closing the reader or connection. 为避免这种情况,请确保在关闭阅读器或连接之前调用取消。

Meaning that the cancel method will work, If you call it before the call to the close() method. 这意味着cancel方法将起作用, 如果在调用close()方法之前调用它。

If you call it after, the command will not be sent to the sql resulting in continues stream to the sql after to call the close() method. 如果你之后调用它,该命令将不会被发送到sql导致继续流到sql之后调用close()方法。

Moreover, if there is a command in process and the attempt to cancel fails, you will not receive an error as no exception is generated in such case. 此外,如果正在进行命令并且取消尝试失败, 则不会收到错误,因为在这种情况下不会生成异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM