What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?
What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?
Is there any difference between SqlCommand.CommandTimeout
and SqlConnection.ConnectionTimeout
in .NET?
回答1
Yes. CommandTimeout
is how long a single command can take to complete. ConnectionTimeout
is how long it can take to establish a connection to the server to start with.
For instance, you may be executing relatively long-running queries - it's perfectly okay for them to take 10 minutes to complete, but if it took 10 minutes to make the connection to start with, you'd know that something was badly wrong.
flipdoubt - the CommandTimeout will affect the query, the ConnectionTimout won't. The ConnectionTimout isn't a timeout for the connection to peform queries - it's just the timeout for the connection to connect to the database in the first place. –回答2
SqlCommand.CommandTimeout
= timeout limit for your SQL query. Means, how much time a (eg: SELECT
, UPDATE
) query can take for its execution. If it exceeds SqlCommand.CommandTimeout
, then it stops execution. A command timeout error will occur.
SqlConnection.ConnectionTimeout