How can I change the table adapter's command timeout

后端 未结 15 1301
遇见更好的自我
遇见更好的自我 2020-12-10 02:12

I\'m using Visual Studio 2008 with C#.

I have a .xsd file and it has a table adapter. I want to change the table adapter\'s command timeout.

Thanks for your

相关标签:
15条回答
  • 2020-12-10 02:39

    You can open up the Properties folder, open Settings.settings and alter the Timeout property of your connection string.

    0 讨论(0)
  • 2020-12-10 02:40

    I do like this ; Right click Fill() or GetX() function and click Goto Defination from menu.

    You will see Source code of DATATABLE. And find ;

    private global::System.Data.SqlClient.SqlCommand[] _commandCollection;
    

    command line from your dataadapter class. And Change the private to public .

    Now you can access the _commandCollection and you can change all attributes.

    But be careful when you add or change any Filed form DESIGNER , the public will be private again by autogenerate system.

    And also , when you finish to call Fill or Get Function you must reset _commandColleciton calling this function ( InitCommandCollection() )

     public void InitCommandCollection() {}
    

    This function is also private by autogen, you must change to public also!

    Example:

    dsIslemlerTableAdapters.tblIslemlerTableAdapter _t = new dsIslemlerTableAdapters.tblIslemlerTableAdapter();
    
    dsIslemler.tblIslemlerDataTable _m = new dsIslemler.tblIslemlerDataTable();
    
    _t._commandCollection[0].CommandText = "Select * From tblIslemler Where IslemTarihi>='' And IslemTarihi<=''";
    
    _m = _t.GetData();
    
    _t.InitCommandCollection();
    
    0 讨论(0)
  • 2020-12-10 02:43

    With some small modifications csl's idea works great.

    partial class FooTableAdapter
    {
      /**
       * <summary>
       * Set timeout in seconds for Select statements.
       * </summary>
       */
      public int SelectCommandTimeout
      {
        set
        {
                for (int i = 0; i < this.CommandCollection.Length; i++)
                    if (this.CommandCollection[i] != null)
                     this.CommandCollection[i].CommandTimeout = value;
        }
      }
    }
    

    To use it, just set this.FooTableAdapter.CommandTimeout = 60; somewhere before the this.FooTableAdapter.Fill();


    If you need to change the timeout on a lot of table adapters, you could create a generic extension method and have it use reflection to change the timeout.

    /// <summary>
    /// Set the Select command timeout for a Table Adapter
    /// </summary>
    public static void TableAdapterCommandTimeout<T>(this T TableAdapter, int CommandTimeout) where T : global::System.ComponentModel.Component
    {                
        foreach (var c in typeof(T).GetProperty("CommandCollection", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance).GetValue(TableAdapter, null) as System.Data.SqlClient.SqlCommand[])
            c.CommandTimeout = CommandTimeout;
    }
    

    Usage:

    this.FooTableAdapter.TableAdapterCommandTimeout(60);
    this.FooTableAdapter.Fill(...);
    

    This is a little slower. And there is the possibility of an error if you use it on the wrong type of object. (As far as I know, there is no "TableAdapter" class that you could limit it to.)

    0 讨论(0)
  • 2020-12-10 02:44

    If you use a partial class, make you have the right namespace. Probably [your data set's name] + "TableAdapters'. Example:

    namespace MyProject.DataSet1TableAdapters

    0 讨论(0)
  • 2020-12-10 02:47

    After scratching my head all day long I finally got the resolution for this. After designing your .xsd file all you need to do is go to your dataset.designer.cs page which is an auto generated page and change the code to provided below. It really works. Give it a try.

    protected global::System.Data.SqlClient.SqlCommand[] CommandCollection
        {
            get
            {
                if ((this._commandCollection == null))
                {
                    this.InitCommandCollection();
                    _commandCollection[0].CommandTimeout = 0;
                }
                _commandCollection[0].CommandTimeout = 0;
                return this._commandCollection;
            }
        }
    
    0 讨论(0)
  • 2020-12-10 02:49

    Here's some example code from MSDN, using VB.NET:

    Imports System.Data.SqlClient
    Namespace MyDataSetTableAdapters
        Partial Class CustomersTableAdapter
            Public Sub SetCommandTimeOut(ByVal timeOut As Integer)
                For Each command As SqlCommand In Me.CommandCollection
                    command.CommandTimeout = timeOut
                Next
            End Sub
        End Class
    End Namespace
    

    When it comes time to call a long query, just call the SetCommandTimeOut method before the query:

    Dim ds As New MyDataSet
    Dim customersTA As New MyDataSetTableAdapters.CustomersTableAdapter
    ' Increase time-out to 60 seconds
    customersTA.SetCommandTimeOut(60000)
    ' Do the slow query
    customersTA.FillSlowQuery(ds.Customers)
    
    0 讨论(0)
提交回复
热议问题