Change timeout for table adapter (call stored procedure) in Visual Studio 2012 VB project?

有些话、适合烂在心里 提交于 2019-12-11 06:56:29

问题


My problem: Call to a Stored Procedure is timing out after 30 seconds. Seems the auto-generated table adapter doesn't provide an interface to change it.

Question: How can I change the timeout value for my SP call?

Environment: Visual Studio 2012, Code in VB.NET, database: SQL Server 2008 R2. I am using a dataset with the DataSet Designer for CRUD operations and also calling Store Procedures. The auto-generated table adapters are called from code.

Research already done: I have found some answers for C# projects, but I have little experience in the language: post 1


回答1:


Since you're using a strongly typed TableAdapter which just holds the DataAdapter as protected property, you cannot change all SqlCommand's CommandTimeouts directly. What you can do is to extend this autogenerated class(it's a Partial Class). Therefore create another class with the same name in the same directory, for example: Public Partial Class YourTableAdapter.

Now you're able to create new properties or methods which can access the DataDapter. Note that the class has to sit in the same (autogenerated) namespace. For example:

Namespace ApplicationName.DataSetNameTableAdapters
    Partial Public Class YourTableAdapter
        Public Property CommandTimeout As Int32
            Get
                Return Me.CommandCollection(0).CommandTimeout
            End Get
            Set(value As Int32)
                For Each cmd As SqlCommand In Me.CommandCollection
                    cmd.CommandTimeout = value
                Next
            End Set
        End Property
    End Class
End NameSpace

Don't extend the original class (.designer.vb), it will be overwritten on every change in the designer.

Now you are able to do:

Dim adapter = new YourTableAdapter()
adapter.CommandTimeout = 60 * 30  ' 30 minutes 


来源:https://stackoverflow.com/questions/26138963/change-timeout-for-table-adapter-call-stored-procedure-in-visual-studio-2012-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!