1

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.

5
  • 1
    This should answer your question: What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout? Commented Feb 14, 2017 at 2:39
  • My question is bit different. SqlConnection.ConnectionTimeout is for timelimit for opening connection. I want to impose max time for reading all records (the total time the while loop can take). Commented Feb 14, 2017 at 3:06
  • What is your reasoning for imposing the max time? What led you to conclude that you need to implement a max timeout? Commented Feb 14, 2017 at 5:57
  • Being a single threaded app, it needs to return quickly from the method that has the while loop to read all records. CommandTimeout does not seem useful because every iteration of while loop has 30 seconds to complete. Commented Feb 14, 2017 at 6:14
  • Does this answer your question? What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout? Commented Jul 10, 2022 at 13:15

1 Answer 1

2

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?

No - a connection timeout is the maximum amount that it will take to open a connection. It has nothing to do with operations after a connection is established.

.That means if you call Read() again, it will have another 30 seconds to complete.

Possibly - it depends on how many network packets each read requires. The sentence before the one you quote states:

This property is the cumulative time-out (for all network packets that are read during the invocation of a method) for all network reads during command execution or processing of the results. For example, with a 30 second time out, if Read requires two network packets, then it has 30 seconds to read both network packets.

It's possible that all of the data could be read in one packet and you'd only need one network read.

If you want a timeout for the while loop you'd need to add a variable and check it within the while loop:

DateTime maxTimeUtc = DateTime.UtcNow.AddSeconds(timeout)
while(rdr.Read())
{
    if(DateTime.UtcNow > maxTimeUtc)
    // do something
}

I would also get in the habit of wrapping the connection, command, and reader in using blocks so that they are disposed of as soon as you are done with them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.