1

I need to set CommandTimeout property for a particular SqlCommand. The premise is that, the main connection string and SqlConnection being used cannot be changed. I like to make defining the timeout value configurable. Wanted to know if its good and fair practice to define it in the app.config (it's a desktop app)?

4
  • A bit of a subjective question, but I don't see a problem with it. Commented Aug 6, 2013 at 17:40
  • But do you want to change the Timeout for every SqlCommand used in your application or just for a single command? Commented Aug 6, 2013 at 17:42
  • @KevinDiTraglia Rather than an absolute problem its just a conundrum for me and wanted to know the best possible way to tackle it :) Commented Aug 6, 2013 at 17:45
  • @Steve For just a single SqlCommand and not for all the commands in the app. Commented Aug 6, 2013 at 17:45

3 Answers 3

4

The app.config is a fantastic place for that type of setting. Maybe something like this:

<appSettings>
  <add key="commandTimeout" value="2" />
</appSettings>

and that would then be in some perspective duration based on how you leverage it. Maybe something like this:

var timeout = cnn.ConnectionTimeout;
var configTimeout = ConfigurationManager.AppSettings["commandTimeout"];
if (!string.IsNullOrEmpty(configTimeout))
{
    timeout = Convert.ToInt32(configTimeout);
}

That could of course exist in a static class, like this maybe:

public static class AppSettings
{
    private static int _commandTimeout = -1;
    public static int CommandTimeout
    {
        get
        {
            if (_commandTimeout != -1) { return _commandTimeout; }

            var configTimeout = ConfigurationManager.AppSettings["commandTimeout"];
            if (!string.IsNullOrEmpty(configTimeout))
            {
                _commandTimeout = Convert.ToInt32(configTimeout);
            }
            else
            {
                _commandTimeout = 1; // this is the default if the setting doesn't exist
            }

            return _commandTimeout;
        }
    }
}

Then all you'd have to do is:

var timeout = AppSettings.CommandTimeout;

or even more concisely:

cmd.CommandTimeout = AppSettings.CommandTimeout;
Sign up to request clarification or add additional context in comments.

2 Comments

Default timeout for SqlCommand.CommandTimeout Property is 30 seconds :)
@rageit, that's true, I was just throwing numbers into my example.
0

You can use the CommandTimeout property and a Time Paramater in the application config file.

connection = Factory.CreateConnection();
connection.ConnectionString = ConnectionString.ConnectionString;
connection.Open();

this.cmd = connection.CreateCommand();
cmd.CommandTimeout = ConfigurationManager.AppSettings["TimeConfigParam"];

Comments

0

I would extend @mike-perrenoud's answer with using TimeSpan as type for the AppConfig option value. It is more readable and understandable for further adjustments. The option could be added via Project Properties -> Settings tab. So that the code would like

SqlCommand command = connection.CreateCommand();
command.CommandTimeout = (int)Properties.Settings.Default.SqlCommandTimeout.TotalSeconds;

app.config

<setting name="SqlCommandTimeout" serializeAs="String">
  <value>00:05:00</value>
</setting>

Properties/Settings.settings

// generated by VS
<Setting Name="SqlCommandTimeout" Type="System.TimeSpan" Scope="Application">
  <Value Profile="(Default)">00:05:00</Value>
</Setting>

Properties/Settings.Designer.cs

// generated by VS
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("00:05:00")]
public global::System.TimeSpan SqlCommandTimeout {
  get {
    return ((global::System.TimeSpan)(this["SqlCommandTimeout"]));
  }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.