I am trying to understand this property, I have a function for running an sql query in ADO.net where I have set the SqlCommand.ConnectTimeout property to 500 seconds and the SqlCommand.CommandTimeout property to 1 second. Roughly the relevant portion looks like this.
var conn = dbConn as SqlConnection;
conn.ConnectTimeout = 500;
conn.StatisticsEnabled = true;
try
{
using (var command = new SqlCommand(query, conn))
{
command.CommandTimeout = 1;
using (var adapter = new SqlDataAdapter(command))
{
var rowsCount = adapter.Fill(startIdx, MaxRows, new DataTable[] { dtResult });
var stats = conn.RetrieveStatistics();
long commandExecutionTimeInMs = (long)stats["ExecutionTime"];
long commandNetworkServerTimeInMs = (long)stats["NetworkServerTime"];
Console.Writeline("Command statistics execution time: " + commandExecutionTimeInMs.ToString());
Console.Writeline("Command statistics network server time: " + commandNetworkServerTimeInMs.ToString());
conn.ResetStatistics();
conn.StatisticsEnabled = false;
return rowsCount;
}
}
}
catch (Exception ex)
{
var stats = conn.RetrieveStatistics();
long commandExecutionTimeInMs = (long)stats["ExecutionTime"];
long commandNetworkServerTimeInMs = (long)stats["NetworkServerTime"];
Console.Writeline("Command statistics execution time: " + commandExecutionTimeInMs.ToString());
Console.Writeline("Command statistics network server time: " + commandNetworkServerTimeInMs.ToString());
conn.ResetStatistics();
conn.StatisticsEnabled = false;
}
With this, I am expecting any execution time above 1 second to timeout and throw an exception with this message: "Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.". However it is very inconsistent, my logs show me that execution time can go up to several seconds or even 10+ seconds without throwing anything sometimes. And NetworkServerTime is consistently in the 200 to 300ms range.
So what is the problem here? Am I misunderstanding how CommandTimeout works? Am I looking at the wrong statistics value? I am looking to set CommandTimeout to 1, and then when my query exceeds this time I can clearly see a statistic that logs the time as 1s or 1000ms in the catch block
Referring to this page nothing else seems to be relevant https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/provider-statistics-for-sql-server