C# 从配置文件定义SqlCommand.CommandTimeout

C# 从配置文件定义SqlCommand.CommandTimeout,c#,.net,ado.net,C#,.net,Ado.net,我需要为特定的SqlCommand设置CommandTimeout属性。前提是,不能更改正在使用的主连接字符串和SqlConnection。我喜欢将定义超时值设置为可配置的。想知道在app.config(这是一个桌面应用程序)中定义它是否是一个好的、公平的做法吗?对于这种类型的设置,app.config是一个很棒的地方。也许是这样的: <appSettings> <add key="commandTimeout" value="2" /> </appSettin

我需要为特定的
SqlCommand
设置
CommandTimeout
属性。前提是,不能更改正在使用的主连接字符串和
SqlConnection
。我喜欢将定义超时值设置为可配置的。想知道在app.config(这是一个桌面应用程序)中定义它是否是一个好的、公平的做法吗?

对于这种类型的设置,
app.config
是一个很棒的地方。也许是这样的:

<appSettings>
  <add key="commandTimeout" value="2" />
</appSettings>
var timeout = cnn.ConnectionTimeout;
var configTimeout = ConfigurationManager.AppSettings["commandTimeout"];
if (!string.IsNullOrEmpty(configTimeout))
{
    timeout = Convert.ToInt32(configTimeout);
}
这当然可能存在于静态类中,比如:

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;
        }
    }
}
那么你所要做的就是:

var timeout = AppSettings.CommandTimeout;
或者更简洁地说:

cmd.CommandTimeout = AppSettings.CommandTimeout;

app.config
是这种设置的绝佳场所。也许是这样的:

<appSettings>
  <add key="commandTimeout" value="2" />
</appSettings>
var timeout = cnn.ConnectionTimeout;
var configTimeout = ConfigurationManager.AppSettings["commandTimeout"];
if (!string.IsNullOrEmpty(configTimeout))
{
    timeout = Convert.ToInt32(configTimeout);
}
这当然可能存在于静态类中,比如:

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;
        }
    }
}
那么你所要做的就是:

var timeout = AppSettings.CommandTimeout;
或者更简洁地说:

cmd.CommandTimeout = AppSettings.CommandTimeout;

可以在应用程序配置文件中使用CommandTimeout属性和时间参数

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

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

可以在应用程序配置文件中使用CommandTimeout属性和时间参数

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

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

我将扩展@mike perrenoud的答案,使用TimeSpan作为AppConfig选项值的类型。对于进一步的调整,它更具可读性和可理解性。 可以通过
项目属性
->
设置选项卡
添加该选项。这样代码就会

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>

我将扩展@mike perrenoud的答案,使用TimeSpan作为AppConfig选项值的类型。对于进一步的调整,它更具可读性和可理解性。 可以通过
项目属性
->
设置选项卡
添加该选项。这样代码就会

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>

有点主观的问题,但是我看不出有什么问题。但是你想更改应用程序中使用的每个SqlCommand的超时时间还是只更改一个命令的超时时间?@KevinDiTraglia不是一个绝对的问题,它只是我的一个难题,我想知道解决这个问题的最佳方法:)@Steve只更改一个SqlCommand,而不是应用程序中的所有命令应用程序。有点主观问题,但是我看不出有什么问题。但是你想更改应用程序中使用的每个SqlCommand的超时时间还是只更改一个命令的超时时间?@KevinDiTraglia不是一个绝对的问题,它只是我的一个难题,我想知道解决这个问题的最佳方法:)@Steve只更改一个SqlCommand,而不是应用程序中的所有命令SqlCommand.CommandTimeout属性的app.Default timeout是30秒:)@rageit,没错,我只是在示例中添加了数字。SqlCommand.CommandTimeout属性的默认超时是30秒:)@rageit,没错,我只是在示例中添加了数字。