Search code examples
c#datasettimeoutcommand

How can I change the table adapter's command timeout


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 help.


Solution

  • 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);
            }
        }
    }