views:

562

answers:

2

Hello everyone,

I am using ADO.Net + C# + VSTS 2008 + ADO.Net to connect to SQL Server 2008 Enterprise. I am using almost the same pattern/sample mentioned here -- using ADO.Net DataReader to retrieve data one entry (row) by one entry (row).

http://msdn.microsoft.com/en-us/library/haa3afyz.aspx

My question is, if I set the SqlCommand timeout in this sample, 1. I think the timeout applies to how much time we could use as maximum value to retrieve one specifc row, not the total timeout for the whole data entry-by-entry loop?

BTW: loop I mean,

while (reader.Read())
{
    Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
        reader.GetString(1));
}

2. and this timeout only matters how much time it takes to retrieve data entry from database, and this timeout has nothing to do with how much time we deal with each entry (e.g. if we set timeout to 20 seconds, and if it takes 1 second to retrieve one data entry from database, and it takes 30 seconds for my application logics to manipulate the data entry, timeout will never happen).

Correct understanding?

+1  A: 

Yes you are right. The CommandTimeout means the Time the Database needs to execute the command (any command)

Arthur
What confuses me is, for DataReader SqlCommand, we could retrieve data entry one by one, but for other types SqlCommand, we retrieve data at one time. So, for DataReader, timeout applies only for one data entry, not for the whole data entry retrieval loop? Appreciate if you could confirm. :-)
George2
+4  A: 

The command timeout that you can set applies to how long you give ADO.NET to do its job.

If you call cmdQuery.ExecuteNonQuery() which returns nothing but performs a SQL statement it's the time needed to perform that statement.

If you call cmdQuery.ExecuteReader() which returns a data reader, it's the time needed for ADO.NET to ste up / construct that data reader so that you can then use it.

If you call cmdQuery.ExecuteScalar() which returns a single scalar value, it's the time needed to execute the query and grab that single result.

If you use the dataAdapter.Fill() to fill a data table or data set, it's the time needed for ADO.NET to retrieve the data and then fill the data table or data set.

So overall : the timeout applies to the portion of the job that ADO.NET can do - execute the statement, fill a data set, return a scalar value.

Of course it does NOT apply to the time it takes YOU to iterate through the results (in case of a data reader). That wouldn't make sense at all...

Marc

marc_s
Thanks Marc, 1. do you mean for the scenario of DataReader, when I receive the DataReader instance, the timeout will not take any effect? 2. If yes, my confusion is, I read some documents before that for DataReader, ADO.Net retrieves data in a streaming fasion, i.e. not all data are retrieved from SQL Server when DataReader is created. If this is true, then there should be further more transfer between ADO.Net client and SQL Server server side? But as you said the timeout does not impact any more after DataReader is created, so no timeout control for further streaming transfer?
George2
My concern for my above comments is, come clients will hold DataReader for a very long time, and I think in streaming mode (since ADO.Net can not predict whether client wants to read more data or just break the reader loop), the connection will be kept alive all the time. I want to have a timeout control for such behavior which force such long live reader to close/release connection from ADO.Net. Any seems the SqlCommand (used to create DataReader) timeout is not what I am looking for? Thanks.
George2
You're absolutely right: if you keep the DataReader open for a long time, the underlying connection will be kept open for that same time. There's no timeout to stop you from doing this - it's your responsability to make sure you don't make this too long a time
marc_s
Thanks Marc, then I have to develop some customized code. Question answered.
George2
+1 - Your answer is more precise
Arthur