I want to set the querytimeout from the connection string. not the connection timeout, is it possible?
8 Answers
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?
2 Comments
You have always been able to specify the Connect Timeout via the SqlClient connection string, this applies to establishing a connection with the database server, not executing commands / running queries. The default Connect Timeout is 15 seconds.
With the release of Microsoft.Data.SqlClient v2.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.
In order to use this new feature, with EF Core 3 and 5, you must add an explicit dependency on the updated SqlClient package by adding the following to your project file:
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
In addition, you must update your connection string in order to increase the default command timeout - keep in mind that this will apply to your entire application, unless overridden in code by setting the SqlCommand.CommandTimeout property.
Connection string examples:
"YourDatabaseAlias": "Server={serverURL}; Initial Catalog={db}; Integrated Security=true; Command Timeout=60"
The connection string above sets the command timeout to 1 minute (60 seconds).
Hope this is useful.
Comments
See:- ConnectionStrings content on this subject. There is no default command timeout property.
Comments
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; }
}
}
}
}
//....
}