David Thielen
David Thielen

Reputation: 32976

SqlDataReader Close takes 10+ seconds - why?

I have a case where on a pretty regular select the call to SqlDataReader.Close() takes 10+ seconds to return. The only thing unusual is the select returns 20 rows but in this case none of the rows are read.

DbDataReader reader = (DbDataReader) cmd.ExecuteReader(CommandBehavior.Default);
reader.MoveNext();
var row = de.Current;

// lots of other code that does not touch this.
reader.Close();

Why does it take so long? And more important, what can I do to make it fast?

Upvotes: 2

Views: 871

Answers (1)

David Thielen
David Thielen

Reputation: 32976

I couldn't find out what/why this is occurring. So on the call to close my object, I create a worker thread and have that close the data reader and connection. I then return immediately so the user sees no delay. Works fine.

Update: As per SqlDataReader.Close() it can be reduced by calling the Cancel method of the associated SqlCommand object before calling the Close method. h/t Lingaraj Mishra

Upvotes: 3

Related Questions