Search code examples
sqlconnection-stringconnection-timeout

sql: set connection timeout in the connection string


In a console app I have a connection string like this:

Data Source=localhost;Initial Catalog=database_name;Integrated Security=True;Connect Timeout=100

I've read here that I need to add Connect Timeout to the connection string to set the timeout, but if I run a query that contains

WAITFOR DELAY '00:00:40';

I'll get a timeout exception, so it seems that the timeoput from the connection string is ignored. so how to set the timeout in the connection string (and not on the connection or command objects)?


Solution

  • There are two timeouts relating to SQL connections/commands - there is a connection timeout, that affects how long a connection is willing to wait when you try to open it, and a command timeout that affects how long an individual command being executed will wait.

    You need to adjust the second of these - by e.g. setting the CommandTimeout property on the SqlCommand object.