How can I change the table adapter's command timeout

后端 未结 15 1402
遇见更好的自我
遇见更好的自我 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:56

    I have investigated this issue a bit today and come up with the following solution based on a few sources. The idea is to create a base class for the table adapter too inherit which increases the timeout for all commands in the table adapter without having to rewrite too much existing code. It has to use reflection since the generated table adapters don't inherit anything useful. It exposes a public function to alter the timeout if you want to delete what i used in the constructor and use that.

    using System;
    using System.Data.SqlClient;
    using System.Reflection;
    
    namespace CSP
    {
        public class TableAdapterBase : System.ComponentModel.Component
        {
            public TableAdapterBase()
            {
                SetCommandTimeout(GetConnection().ConnectionTimeout);
            }
    
            public void SetCommandTimeout(int Timeout)
            {
                foreach (var c in SelectCommand())
                    c.CommandTimeout = Timeout;
            }
    
            private System.Data.SqlClient.SqlConnection GetConnection()
            {
                return GetProperty("Connection") as System.Data.SqlClient.SqlConnection;
            }
    
            private SqlCommand[] SelectCommand()
            {
                return GetProperty("CommandCollection") as SqlCommand[];
            }
    
            private Object GetProperty(String s)
            {
                return this.GetType().GetProperty(s, BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance).GetValue(this, null);
            }
        }
    }
    

提交回复
热议问题