SqlCommand.Cancel() causes a performance boost?

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 just a standard procedure of running a SqlCommand):

//SqlCommand cmd = new SqlCommand();
//cmd.ExecuteReader();
//Read off the results

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

Basically, after finishing a query, it goes back and cancels it, claiming some performance boost. I suppose you might get some memory back when it goes and frees up the XmlReader, but usually it's about to go out of scope anyways.

I've never bothered with it before, but it's finally showed up in some code I'm reviewing. Does canceling a SqlCommand after running it in the code actually somehow speed it up, or is this just some weird programmer superstition?

11
задан fire.eagle 17 August 2010 в 19:05
поделиться

1 ответ

Согласно MSDN , это правильно.

Метод Close заполняет значения для выходных параметров возвращаемые значения и RecordsAffected, увеличивая время, необходимое для закрытия SqlDataReader, который использовался для обработки большой или сложный запрос. Когда возвращаемые значения и количество записи, затронутые запросом, не значительный, время, необходимое для закрыть SqlDataReader можно уменьшить вызывая метод Cancel объекта связанный объект SqlCommand до вызов метода Close.

Странно!

15
ответ дан 3 December 2019 в 04:50
поделиться
Другие вопросы по тегам:

Похожие вопросы: