sql-servercommand-timeout

manually increasing SQL server command timeout


I had a test windows host (50 MB) which was serving my ASP.NET web app, it worked almost fine, but after it was upgraded to 100 MB, I got frequent SQL server timeout errors when trying to remotely connect SQL server from Visual Studio (i.e. in local development of my site), I tried to manually increase SQL commands timeout, and I think it was somehow solved, but I think my action is not right, of course my site viewers have no problem in viewing the pages as everything works fine for final users, I only had problems while developing the site and accessing my remote database via VS2010, should I really consider increasing timeout? how can I globally increase the timeout? should I set commandtimeout property for each SQL command? is it possible to increase it in connection string? of course as I'm currently using a shared host I cannot change SQL server management studio settings

please guide me about this problem as I don't have a good feeling regarding the timeout property! thanks


Solution

  • I don't think there's any easy way to set this globally - e.g. in the connection string or something.

    What you could do (but this requires your code to work that way) is put your command timeout into configuration:

    <configuration>
       <appSettings>
          <add name="CommandTimeout" value="120" />
       </appSettings>
    </configuration>
    

    and then in your code, read that value from configuration and use it for your SqlCommand classes:

    int commandTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["CommandTimeout"]);
    
    using(SqlCommand cmd = new SqlCommand(..(your SQL statement)..., connection))
    {
        cmd.CommandTimeout = commandTimeout;
    }
    

    With this, you could at least tweak the timeout without having to change your code.