【问题标题】:QueriesTableAdapter CommandTimeoutQueriesTableAdapter 命令超时
【发布时间】:2013-09-30 17:40:11
【问题描述】:

我有一个带有更新存储过程的 QueriesTableAdapter 需要很长时间才能执行。我正在尝试增加调用存储过程的查询表适配器的命令超时属性。

存储过程是一种维护例程,一个进程通过网络服务不经常调用它。

请注意:它是 QueriesTableAdapter 而不是 TableAdapter,我使用的是 .net 4.5 VS 2012

在我的 DAL 后面的代码中,我添加了以下内容:

Namespace mbr_AccountTableAdapters
    Partial Public Class QueriesTableAdapter

        ''' <summary>
        ''' Sets the commant timeout.
        ''' Set to 0 to specify the longest timout value supported by the db.
        ''' </summary>
        Public WriteOnly Property CommandTimeout As Integer
            Set(value As Integer)
                If Not IsNothing(Me._commandCollection) Then
                    For Each cmd As System.Data.IDbCommand In Me._commandCollection
                        cmd.CommandTimeout = value
                    Next
                End If
            End Set
        End Property

    End Class
End Namespace

然后我可以使用我的 BLL 中的以下代码运行我的存储过程:

Dim rows As Integer = 0 'number of affected rows

Using myQTA As New DAL.mbr_AccountTableAdapters.QueriesTableAdapter

    'increase the command timeout:
    myQTA.CommandTimeout = 0 '0 = larget value possible

    Dim queryResult As Object
    queryResult = myQTA.usrsp_mbr_account_CleanupInactive()
    If Not IsNothing(queryResult) Then
        rows = Convert.ToInt32(queryResult)
    End If

End Using

我收到以下错误:

[Win32Exception (0x80004005): 等待操作超时]

[SqlException (0x80131904): 超时。在操作完成之前超时时间已过或服务器没有响应。]


请注意,查询有效 - 连接没有问题或任何其他错误。如果我将需要处理的数据量减少到很小的数据子集,则查询运行不会出错。

问题是当它需要超过 30 秒时。

查询恰好在 30 秒后超时。

在 web.config 中的数据库连接字符串中添加 'Connection Timeout=90' 没有区别,30 秒后仍然超时。

【问题讨论】:

    标签: asp.net sql-server ado.net


    【解决方案1】:

    我设法解决了我的问题。

    _commandCollection 始终为 NULL,因此永远不会设置 CommandTimeout 值。

    我们必须先调用 InitCommandCollection() 来初始化命令集合,然后我们才能设置 CommandTimeout 值:

    我将代码更改为:

    Public WriteOnly Property CommandTimeout As Integer
        Set(value As Integer)
    
            'initialize the collection first:
            If IsNothing(Me._commandCollection) Then
                Me.InitCommandCollection()
            End If
    
            If Not IsNothing(Me._commandCollection) Then
                For Each cmd As System.Data.IDbCommand In Me._commandCollection
                    cmd.CommandTimeout = value
                Next
            End If
        End Set
    End Property
    

    它现在按预期工作,我可以为存储过程设置自定义超时值。

    我希望这些信息对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 2013-04-03
      • 2013-09-30
      • 2019-12-06
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多