Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the query timeout from SQL connection string

Tags:

c#

sql-server

People also ask

How do I set query timeout in connection string?

1 it's introduced the "Command Timeout" connection string property to override, if required, the default of 30 seconds for this property. Hence it is now possible to set the default command timeout via the connection string. The connection string above sets the command timeout to 1 minute (60 seconds).

How do you stop a query from timing out?

How can I prevent the query from timing out? Answer: Open your query in design view. Then right-click in a blank area of the design view (where the tables are displayed) and select Properties from the popup menu. When the "Query Properties" window appears, set the "ODBC Timeout" property to 0.


No. It's per command, not per connection.

Edit, May 2013

As requested in comment:

  • SQLCommand.CommandTimeout for command execution
  • There is no matching SQLConnection property (the questions says not the SqlConnection.ConnectionTimeout property

Some more notes about commands and execution time outs in SQL Server (DBA.SE). And more SO stuff: What happens to an uncommitted transaction when the connection is closed?


See:- ConnectionStrings content on this subject. There is no default command timeout property.


You can only set the connection timeout on the connection string, the timeout for your query would normally be on the command timeout. (Assuming we are talking .net here, I can't really tell from your question).

However the command timeout has no effect when the command is executed against a context connection (a SqlConnection opened with "context connection=true" in the connection string).


Only from code:

namespace xxx.DsXxxTableAdapters {
    partial class ZzzTableAdapter
    {
        public void SetTimeout(int timeout)
        {
            if (this.Adapter.DeleteCommand != null) { this.Adapter.DeleteCommand.CommandTimeout = timeout; }
            if (this.Adapter.InsertCommand != null) { this.Adapter.InsertCommand.CommandTimeout = timeout; }
            if (this.Adapter.UpdateCommand != null) { this.Adapter.UpdateCommand.CommandTimeout = timeout; }
            if (this._commandCollection == null) { this.InitCommandCollection(); }
            if (this._commandCollection != null)
            {
                foreach (System.Data.SqlClient.SqlCommand item in this._commandCollection)
                {
                    if (item != null)
                    { item.CommandTimeout = timeout; }
                }
            }
        }
    }
 
    //....
 
 }