Question

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

Was it helpful?

Solution

I made it like this:

private readonly MyDbContext _context;

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

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

    }

OTHER TIPS

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.

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;       

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.

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

You can add connection time out in your web.config file. Set it to 0 to have it infinite as below.

Connection Timeout=0;

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

Command Timeout=60;

instead of:

CommandTimeout=60;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top