I cannot seem to get the CommandTimeout to work in the code below. I set the timeout value to 1 second just to test to ensure it works but it seems to be ignored. The SELECT statement takes about 15 seconds to run and it runs to completion. I am expecting a timeout exception to occur after 1 second. I have searched the internet to find an example of how to do this and I have not been able to find anything. What am I doing wrong?
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
DataSet dataSet = new DataSet();
DbCommand command = factory.CreateCommand();
command.Connection = factory.CreateConnection();
command.Connection.ConnectionString = "Server=localhost;Database=MyDatabase;User Id=;Password=****";
command.CommandType = CommandType.Text;
command.CommandText = "SELECT * FROM table";
command.CommandTimeout = 1;
using (DbDataAdapter adapter = factory.CreateDataAdapter())
{
adapter.SelectCommand = command;
adapter.Fill(dataSet);
}
UPDATE:
I wrote a SELECT command that takes longer than 30 seconds to run. This also does not timeout. I even commented out my CommandTimeout line and it still ran to completion. So, it seems that even the default CommandTimeout of 30 seconds is being ignored. I am stumped...any help would be greatly appreciated.
UPDATE:
I think I might have figured it out. It seems when I wrote a really complex SELECT statement the command timeout exception occurred. I can only guess that what is happening it that, with my simple SELECT command, it is taking less than 1 second to execute the command but then it takes an additional 15 seconds to load the data set. Am I on the right track here? Does this seem likely?