19

How to add CommandTimeout to connection string in web.config?

I tried:

<add name="ConnectionString" connectionString="Data Source=;Initial Catalog=;Persist Security Info=True;User ID=sa;Password=sa@123;Connect Timeout=200" providerName="System.Data.SqlClient"/>

</connectionStrings>

and this:

<add name="MyProject.ConnectionString"  
         connectionString="Data Source=127.0.0.1;Initial Catalog=MyDB;Persist Security Info=True;CommandTimeout=60;User ID=sa;Password=saPassw0rd"
         providerName="System.Data.SqlClient" />

but it didnt' work for me.

Thanks

1

6 Answers 6

12

I made it like this:

private readonly MyDbContext _context;

    public LinqToSql() : this(new MyDbContext())
    {
    }

    private LinqToSql(MyDbContext context)
    {
        _context = context;
        _context.CommandTimeout = 500; 

    }
Sign up to request clarification or add additional context in comments.

Comments

9

As far as I know there is no global way of setting Command timeout property, you have to set CommandTimeout property individually for each command object you create.

Comments

2

You can set the timeout in a setting and refer to that value when the command timeout is set.
In config under appSettings add a key for CommandTimeout:

<add key="ContextCommandTimeout" value="500" />

Then in your code:

int cmdTimeout = -1;
string timeoutSettings = ConfigurationManager.AppSettings["ContextCommandTimeout"];

if(!string.IsNullOrEmpty(timeoutSettings)) 
{
    int.TryParse(timeoutSettings, out cmdTimeout);
}

if(cmdTimeout >=0)
    _context.CommandTimeout = cmdTimeout;       

Comments

2

For those still interested, as of about October 2020 it looks like this might be possible with .NET EF Core.

With the latest 2.1.0 preview 2 release of the open source .NET client driver for Microsoft SQL Server and Azure SQL Database, Microsoft.Data.SqlClient, it is now possible to set the default command timeout via the connection string.

Now you can work around timeout issues simply by changing the connection string, where this previously required changes to code, and maybe changes to code you did not have the ability to change.

This could be an issue when you were using third party tools like Entity Framework Core migrations or EF Core database reverse engineering (scaffolding).

Refer: https://erikej.github.io/sqlclient/2020/10/26/sqlclient-commandtimeout-preview.html

Comments

0

Going to assume this is a .NET web-app, since it looks like one & it wasn't specified in the description / tags. This works for an app I've been developing, sets the DB command timeout to be 15 seconds:

<connectionStrings>
  <add name="ConnectionString" 
       connectionString="Server=localhost; Port=3306; Database=<DB>;
                         uid=<usr>; pwd=<pw>; default command timeout=15;" 
       providerName="MySql.Data.MySqlClient" />
</connectionStrings>

The default command timeout is applied app-wide to each individual DB request. As far as I'm aware, the default is 30 seconds if this isn't specified. For a web-app, individual DB commands that take longer than 30 seconds is a bit extreme though; you may be better served having a rethink of how you architected the solution.

2 Comments

It should be noted that this only works because the MySql ConnectionStrings element allows that property. Its not available for MSSQL for instance (so not supported using System.Data.SqlClient)
It doesn't work for SQL Azure
-6

If you want to put it into your web.config, you need to add a space. Try:

Command Timeout=60;

instead of:

CommandTimeout=60;

2 Comments

It doesn't work when adding to SqlClient provider connection string, I am getting: ArgumentException: Keyword not supported: 'command timeout'
SQLConnection objects appear to have a command timeout, but not for the ConnectionString: learn.microsoft.com/en-us/dotnet/api/… learn.microsoft.com/en-us/dotnet/api/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.