In my C# reporting program I had a query which was giving me timeouts. The part that gave me trouble was something similar to:
SELECT *
FROM invoices
WHERE processdate BETWEEN @from AND @until
AND printdate > accountingdate
If I defined the @from and @until variables in the query, i.e. if I prepend it with:
DECLARE @from datetime = '2015-01-08'
DECLARE @until datetime = '2015-01-14'
everything ran very fast as expected.
But when I run the query from my C# program the parameters get added like this: