April 2003
Beginner to intermediate
620 pages
21h 48m
English
CommandTimeout
Int32timeout= Command.CommandTimeout; Command.CommandTimeout =timeout;
Configures the time in seconds that a command will wait once you
execute it. If the command hasn’t completed once the
timeout is reached, the attempt is aborted, and a provider-specific
exception (such as SqlException or
OleDbException) is thrown.
The default timeout is 30 seconds. You can set the timeout to 0 to
specify an infinite timeout, but this isn’t
recommended because it can stall your application indefinitely. You
can call the Command.Cancel( ) method from a
separate thread to halt an in-progress command.
The following code fragment defines a timeout of 15 seconds:
SqlCommand cmd = new SqlCommand(commandText, con);
cmd.CommandTimeout = 15;
try
{
con.Open();
// (Now execute the command)
}
catch (SqlException err)
{
// This could indicate a timeout after 15 seconds.
}
finally
{
con.Close();
}Read now
Unlock full access