Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在App.config中设置SQLCommandTimeout_C#_Sql - Fatal编程技术网

C# 如何在App.config中设置SQLCommandTimeout

C# 如何在App.config中设置SQLCommandTimeout,c#,sql,C#,Sql,我已经开发了一个使用SQL数据库的窗口服务,目前我的数据库中充满了记录,所以查询执行需要花费很多时间,而默认命令超时是30秒,但我想将其增加到120秒一个选项是 com.CommandTimeout = 120; 但我的应用程序中有很多方法,所以我想从APP.config文件中设置它,以便它适用于应用程序级别,有人能告诉我如何实现这一点吗 谢谢在app.config文件中使用此文件 <configuration> <appSettings> <add

我已经开发了一个使用SQL数据库的窗口服务,目前我的数据库中充满了记录,所以查询执行需要花费很多时间,而默认命令超时是30秒,但我想将其增加到120秒一个选项是

com.CommandTimeout = 120;
但我的应用程序中有很多方法,所以我想从APP.config文件中设置它,以便它适用于应用程序级别,有人能告诉我如何实现这一点吗


谢谢在app.config文件中使用此文件

<configuration>
  <appSettings>
    <add key="connectioStringName" value="Data Source=source;Initial Catalog=tableName;User Id=databaseName;Password=password;Connection Timeout=3000"/>
  </appSettings>
</configuration>

实现这一点的最简单方法是在
中添加一个新条目,如下所示:

<appSettings>
    <add key="commandTimeout" value="3000" />
</appSettings>
现在,在您的代码中,不只是实例化一个新的
SqlCommand
,只需调用
CommandFactory
方法:

using(var command = CommandFactory.CreateCommand(yourConnection))
{
    //
}

没有可在app中配置的此类设置。Config在appSettings下添加新的命令超时设置。这将设置
SqlConnection
ConnectionTimeout
属性,而不是命令超时。见MSDN
using(var command = CommandFactory.CreateCommand(yourConnection))
{
    //
}