What Is "Connect Timeout" in SQL Server Connection String

What is Connect Timeout in sql server connection string?

That is the timeout to create the connection, NOT a timeout for commands executed over that connection.

See for instance http://www.connectionstrings.com/all-sql-server-connection-string-keywords/
(note that the property is "Connect Timeout" (or "Connection Timeout"), not just "Timeout")


From the comments:

It is not possible to set the command timeout through the connection string. However, the SqlCommand has a CommandTimeout property (derived from DbCommand) where you can set a timeout (in seconds) per command.

Do note that when you loop over query results with Read(), the timeout is reset on every read. The timeout is for each network request, not for the total connection.

sql: set connection timeout in the connection string

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.

Connection timeout for SQL server

Yes, you could append ;Connection Timeout=30 to your connection string and specify the value you wish.

The timeout value set in the Connection Timeout property is a time expressed in seconds. If this property isn't set, the timeout value for the connection is the default value (15 seconds).

Moreover, setting the timeout value to 0, you are specifying that your attempt to connect waits an infinite time. As described in the documentation, this is something that you shouldn't set in your connection string:

A value of 0 indicates no limit, and should be avoided in a
ConnectionString because an attempt to connect waits indefinitely.

Cannot configure timeout from connection string

Use CommandTimeout:

using (var command = new SqlCommand("dbo.test", connection) {    
CommandType = CommandType.StoredProcedure })
{
connection.Open();
Console.WriteLine("ConnectionTimeout: {0}",
connection.ConnectionTimeout);
command.CommandTimeout = 120;
command.ExecuteNonQuery();
Console.WriteLine("Finished");
connection.Close();
}

Changing SqlConnection timeout at runtime

you can also alter the connstring itself (might be a little overkill)

        string constring = "Data Source=.\\SQL2016;Initial Catalog=MyDatabase;Persist Security Info=False;User ID=DbUser;Password=DbPassword;Connection Timeout=6\" providerName=\"System.Data.SqlClient";
int index = constring.IndexOf("Connection Timeout=");
var oldTimeOut = new String(constring.Substring(index).Split('=')[1].Where(Char.IsDigit).ToArray());
constring = constring.Replace("Connection Timeout="+oldTimeOut, "Connection Timeout=" + newTimeOut);

Default Timeout Period in SQL Server

DbCommand has CommandTimeout, which is what you want here - it is set per command; the "connect timeout" only impacts, reasonably enough, what the timeout is for connecting. The default value for CommandTimeout on SqlCommand is 30 seconds.

IIS connection timeout and connection string timeout attribute

DB connection timeout is very diffrent from IIS time out

DB connection timeout is about establis the connection to DB, from MSDN SqlConnection.ConnectionTimeout

Gets the time to wait while trying to establish a connection before
terminating the attempt and generating an error.

From MSDN about Connection Timeout

Connection timeouts help reduce the amount of memory resources that
are consumed by idle connections. Time-out settings also allow you to
specify how long server resources are allocated to specific tasks or
clients.

SQL Timeout is longer than I set it

https://stackoverflow.com/a/50944424/5507322 - Fair point, but it does not work in all scenarios.

An explanation of this above, why it is not reliable and the solution to my problem:
http://improve.dk/controlling-sqlconnection-timeouts/



Related Topics



Leave a reply



Submit