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 he did not work for me.

thanks

+8
web-config timeout connection-string connection-timeout
source share
4 answers

I did it like this:

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

As far as I know, there is no global way to set the timeout Command property, you must set the CommandTimeout property individually for each command object created.

+6
source share

You can set a timeout in the configuration and refer to this value when the command timeout is set.
In the config under appSettings add the 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; 
+1
source share

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

 Command Timeout=60; 

instead:

 CommandTimeout=60; 
-one
source share

All Articles