Suddenly, some querys that I have in a Winforms app are taking too long to execute directly in the code but when I copy the query and execute it in MSSQL it takes 0.02 ms. I can't figure out what is going on. This happens not in all my SQL querys and I can't understand if there's a pattern or a logic behind that. Have you already face something like this?
I had the same problem, it turns out that MSSQL uses a different plan than applications build in visual studio.
I ran this query to make all my connections use the same plan, it worked for me.
declare @value sql_variant
select @value = SESSIONPROPERTY('ARITHABORT')
if @value <> 1
begin
USE master
ALTER DATABASE [yourDataBaseName] SET ARITHABORT ON WITH NO_WAIT
use yourDataBaseName
end