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)?
-
A bit of a subjective question, but I don't see a problem with it.Kevin DiTraglia– Kevin DiTraglia2013-08-06 17:40:14 +00:00Commented 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?Steve– Steve2013-08-06 17:42:15 +00:00Commented 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 :)rageit– rageit2013-08-06 17:45:04 +00:00Commented Aug 6, 2013 at 17:45
-
@Steve For just a single SqlCommand and not for all the commands in the app.rageit– rageit2013-08-06 17:45:30 +00:00Commented Aug 6, 2013 at 17:45
3 Answers
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;
2 Comments
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
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"]));
}
}