System.Data.SqlClient Namespace SqlCommand Class
Cancels the execution of an SqlCommand.
[ VB ]
NotOverridable Public Sub Cancel ( )
[ C# ]
public void Cancel ( );
[ C++ ]
public: __sealed void Cancel ( );
[ JScript ]
public function Cancel ( );
IDbCommand.Cancel
If there is nothing to cancel, nothing happens. However, if there is a command in process, and the attempt to cancel fails, no exception is generated.
The following example demonstrates the use of the Cancel method.
private SqlCommand _cmd;
// threads signal these when done
private AutoResetEvent _e1 = new AutoResetEvent ( false );
private AutoResetEvent _e2 = new AutoResetEvent ( false );
void IssueQuery ( ) {
SqlDataReader r;
int rows = 0;
try {
r = _cmd.ExecuteReader ( );
do {
while ( r.Read ( ) ) {
rows++;
}
} while ( r.NextResult ( ) );
Response.Write ( "FAILED: execution should not have finished, {0} rows read!", rows );
}
catch ( ) {
Response.Write ( "PASSED: Got expected exception!" );
}
_e1.Set ( );
}
void CancelQuery ( ) {
System.Threading.Thread.Sleep ( 500 );
_cmd.Cancel ( );
_e2.Set ( );
}
void CancelTest ( ) {
SqlConnection conn = new SqlConnection (
"Data Source=localhost; Integrated Security=SSPI;Initial Catalog=northwind;" );
conn.Open ( );
_cmd = new SqlCommand ( null, conn );
AutoResetEvent [ ] evs = new AutoResetEvent [ 2 ];
evs [ 0 ] = _e1;
evs [ 1 ] = _e2;
Thread t1 = new Thread ( new ThreadStart ( this.IssueQuery ) );
Thread t2 = new Thread ( new ThreadStart ( this.CancelQuery ) );
t1 = new Thread ( new ThreadStart ( this.IssueQuery ) );
t2 = new Thread ( new ThreadStart ( this.CancelQuery ) );
// cancel operation while results are coming back
_cmd.CommandText = "SELECT * FROM orders, products";
t1.Start ( );
t2.Start ( );
WaitHandle.WaitAll ( evs );
conn.Close ( );
}
Private _cmd As SqlCommand
' threads signal these when done
Private _e1 As AutoResetEvent = New AutoResetEvent ( False )
Private _e2 As AutoResetEvent = New AutoResetEvent ( False )
Public Sub IssueQuery ( )
Dim r As SqlDataReader
Dim rows As Integer = 0
Try
r = _cmd.ExecuteReader ( )
Do
Do While r.Read
rows = rows + 1
Loop
Loop While r.NextResult ( )
Response.Write ( "FAILED: execution should not have finished, {0} rows read!", rows )
Catch
Response.Write ( "PASSED: Got expected exception!" )
End Try
_e1.Set ( )
End Sub
Public Sub CancelQuery ( )
System.Threading.Thread.Sleep ( 500 )
_cmd.Cancel ( )
_e2.Set ( )
End Sub
Public Sub CancelTest ( )
Dim conn As SqlConnection = New SqlConnection ( _
"Data Source=localhost; Integrated Security=SSPI;Initial Catalog=northwind;" )
conn.Open ( )
Dim _cmd As SqlCommand = New SqlCommand ( "", conn )
Dim evs ( 2 ) As AutoResetEvent
evs ( 0 ) = _e1
evs ( 1 ) = _e2
Dim t1Start As ThreadStart = New ThreadStart ( AddressOf IssueQuery )
Dim t2Start As ThreadStart = New ThreadStart ( AddressOf CancelQuery )
Dim t1 As Thread = New Thread ( t1Start )
Dim t2 As Thread = New Thread ( t2Start )
' cancel operation while results are coming back
_cmd.CommandText = "SELECT * FROM orders, products"
t1.Start ( )
t2.Start ( )
WaitHandle.WaitAll ( evs )
conn.Close ( )
End Sub |
|
C# |
VB |
SqlCommand Members