-
Notifications
You must be signed in to change notification settings - Fork 316
Closed
Labels
Area\DocumentationUse this for issues that requires changes in public documentations/samples.Use this for issues that requires changes in public documentations/samples.
Description
The remarks section of the SqlDataReader.Close docs says:
You must explicitly call the Close method when you are through using the SqlDataReader to use the associated SqlConnection for any other purpose.
The Close method fills in the values for output parameters, return values and RecordsAffected, increasing the time that it takes to close a SqlDataReader that was used to process a large or complex query. When the return values and the number of records affected by a query are not significant, the time that it takes to close the SqlDataReader can be reduced by calling the Cancel method of the associated SqlCommand object before calling the Close method.
Some notes:
- If Dispose is called, Close isn't needed. This allows SqlDataReader to be used with the preferred using pattern.
- Output parameters seem to be filled in before Close is called.
- I'm not too sure avoiding filling in output/return parameter and RecordsAffected has any significance perf-wise. On the other hand, if the reader still has a big pending resultset which isn't needed, closing/disposing would need to consume it, which indeed has a perf impact, justifying a possible command cancellation instead. However, especially since cancellation has its own problems, the best practice should be to simply not to fetch rows that you don't need (e.g. use
TOP(n)). - Grammar-wise, in
When the return values and the number of records affected by a query are not significant, thenotseems like it should be removed.
Metadata
Metadata
Assignees
Labels
Area\DocumentationUse this for issues that requires changes in public documentations/samples.Use this for issues that requires changes in public documentations/samples.