Control TableAdapter Command Timeout Globally

前端 未结 4 534
失恋的感觉
失恋的感觉 2021-01-03 05:01

I have a DataSet with a QueriesTableAdapter. In order to control the SqlCommand.CommandTimeout I\'ve added a partial class called QueriesTableAdapter with a public method ca

4条回答
  •  抹茶落季
    2021-01-03 05:38

    All generated TableAdapters inherit from Component. Therefore, you could write a method like this that uses reflection to extract the adapter property:

        private void ChangeTimeout(Component component, int timeout)
        {
            if (!component.GetType().Name.Contains("TableAdapter"))
            {
                return;
            }
    
            PropertyInfo adapterProp = component.GetType().GetProperty("Adapter", BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance);
            if (adapterProp == null)
            {
                return;
            }
    
            SqlDataAdapter adapter = adapterProp.GetValue(component, null) as SqlDataAdapter;
            if (adapter == null)
            {
                return;
            }
    
            adapter.SelectCommand.CommandTimeout = timeout;
        }
    

    You then can call it like this:

    MyTableAdapter ta = new MyTableAdapter();
    this.ChangeTimeout(ta,1000);
    

    I'm assuming that since you're using typed DataSet's that you're still in .NET 2.0 which is why I didn't bother making this an extension method.

提交回复
热议问题