【问题标题】:In a `SqlCommand`, what timespan does the `CommandTimeout` actually represent?在“SqlCommand”中,“CommandTimeout”实际代表什么时间跨度?
【发布时间】:2019-03-16 06:18:06
【问题描述】:

我正在使用 SqlConnectionSqlCommandSqlReader 查询 Microsoft SQL 数据库,就像在 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader 中描述的那样

现在,SqlCommand 允许设置 CommandTimeout,我正在这样做(简化):

using (SqlConnection connection = GetConnection()) {
    connection.Open();
    using (SqlCommand command = connection.CreateCommand()) {

        command.CommandText = query; //My custom SQL query
        command.CommandType = CommandType.Text;

        //Set Timeout
        command.CommandTimeout = timeout.Value; //My custom timeout

        using (SqlDataReader reader = command.ExecuteReader()) {
            while (reader.Read()) {
                //Read row by row and do stuff
            }
        }
    }
}

我的问题是,超时实际上适用于什么?是吗

  • ExecuteReader()中花费的时间
  • Read()中花费的时间
  • 这两样
  • 还有别的吗?

MSDN 和网络上似乎都没有这方面的具体文档。

【问题讨论】:

    标签: sql .net timeout sqldatareader sqlcommand


    【解决方案1】:

    它将适用于对数据库执行命令的时间,即command.ExecuteReader(),在您开始读取记录时完成。您可以通过在 while 之后设置一个断点并坐在那里进行测试 - 您不会超时。

    【讨论】:

      猜你喜欢
      • 2011-10-24
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2010-09-24
      • 2018-09-18
      • 2021-08-22
      • 2016-06-18
      相关资源
      最近更新 更多