Search code examples
web-configtimeoutconnection-stringconnection-timeout

How to add CommandTimeout to connection string in web.config


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


Solution

  • I made it like this:

    private readonly MyDbContext _context;
    
        public LinqToSql() : this(new MyDbContext())
        {
        }
    
        private LinqToSql(MyDbContext context)
        {
            _context = context;
            _context.CommandTimeout = 500; 
    
        }