I would like to understand relation between connection timeout and command timeout and how one affects other. Please consider this code as an example.
// declare the SqlDataReader, which is used in
// both the try block and the finally block
SqlDataReader rdr = null;
// create a connection object
SqlConnection conn = new SqlConnection("someconnstr");
// create a command object
SqlCommand cmd = new SqlCommand("select * from dbo.mytable", conn);
try
{
// open the connection
conn.Open();
// 1. get an instance of the SqlDataReader
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
// get the results of each column
Guid Id = (Guid)rdr["Id"];
string displayName = (string)rdr["Name"];
// print out the results
Console.WriteLine("{0}, {1}", Id, displayName);
}
Console.WriteLine("Reading done");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
As per MSDN link, command timeout is a cumulative timeout for all reads. That means if you call Read() again, it will have another 30 seconds to complete. I want to set timeout in such a way that I can impose a maximum timeout for all records.
Is connection timeout a good thing for doing this? In the given example, if I set connection timeout to 120 seconds and the while loop does not finish in 120 seconds, will it throw a timeout error?
This question is related to Stackoverflow question.