1

我有一个 .NET Windows 服务,它引用了一个调用 SQL Server 数据库的特定程序集。这些 SQL 调用的连接字符串被编码为从 ConfigurationManager.ConnectionStrings["myconnection"] 中提取。

在服务的安装目录中,我有一个配置文件,其中包含连接字符串“myconnection”,它最初没有连接超时值,长时间运行的查询在 30 秒后意外超时。但是,通过将“Connection Timeout=300”添加到连接字符串并重新启动服务,我们看不出有什么区别。

编辑: commandtimeout 属性设置为 300。示例如下:

SqlCommand _command     = new SqlCommand(Sql, _con);
            SqlDataReader _DR       = _command.ExecuteReader();
            _command.CommandTimeout = 300;
            string _fieldValue;
            string _columnValue;

            while (_DR.Read()){ ... }
4

1 回答 1

3

您需要将SqlCommand.CommandTimeout设置为更高的值。

SqlCommand.CommandTimeout是单个 sql 命令需要多长时间,而 SqlConnection.ConnectionTimeout建立与 sql server 的连接需要多长时间。

我不相信SqlCommand.CommandTimeout可以从配置文件中设置 - 它必须在SqlCommand本身的每个实例上设置。

于 2012-09-26T15:42:14.950 回答