Control TableAdapter Command Timeout Globally

前端 未结 4 546
失恋的感觉
失恋的感觉 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:45

    for some reason, my adapter's .selectcommand was null so i ended up having to go through the CommandCollection object instead, so i thought i'd post my small change based on the previous answer above.

    includes:

    using System.ComponentModel;
    using System.Reflection;
    

    code:

    private void ChangeTimeout(Component component, int timeout)
            {
                if (!component.GetType().Name.Contains("TableAdapter"))
                {
                    return;
                }
    
                PropertyInfo adapterProp = component.GetType().GetProperty("CommandCollection", BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance);
                if (adapterProp == null)
                {
                    return;
                }           
    
                SqlCommand[] command = adapterProp.GetValue(component, null) as SqlCommand[];
    
                if (command == null)
                {
                    return;
                }
    
                command[0].CommandTimeout = timeout;            
            }
    

提交回复
热议问题