I have seen this show up several places in code, never with an explanation, just a cryptic comment above it (Declaration and execution included for an idea of context. It\'s jus
Calling Cancel
gives a potentially MASSIVE performance improvement if your call to ExecuteReader
returns a large number of rows, and you don't read all the rows.
To illustrate, let's say a query returns a million rows, and you close the reader after reading just the first 1000 rows. If you fail to call Cancel
before closing the reader, the Close
method will block while it internally enumerates through the remaining 999,000 rows
Try it and see!