How to add CommandTimeout to connection string in web.config

user833102 picture user833102 · Dec 19, 2013 · Viewed 70.1k times · Source

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

Answer

user833102 picture user833102 · Jan 6, 2014

I made it like this:

private readonly MyDbContext _context;

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

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

    }