SqlCommand.Cancel before SqlDataReader.Close
I’ve just read that you can improve the performance of your SQL queries to Microsoft SQL Servers using .NET 2.0 by simply cancelling the command before closing your data reader.
using (SqlCommand command = connection.CreateCommand())
{
// Your code here
using (SqlDataReader reader = command.ExecuteReader())
{
// Your code here
command.Cancel();
reader.Close();
}
command.Close();
command.Dispose();
}
This is useful if you do not need the return values sending back through the command, and most commonly you will never need to. I’m not sure of the exact performance gain from doing such a modification to your code but I’m certain that since it was mentioned in the MSDN documentation (under remarks), it must be safe to test at least.
Leave a Reply