Control TableAdapter Command Timeout Globally

人走茶凉 提交于 2019-11-30 15:36:58

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

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.

BFree and mark's similar solutions work well with reflection. Below is a slight refinement that I think yields neater code.

You can also change the base class that the TableAdapter uses in the DataSet designer. You can change your TableAdapter's base class to MyTableAdapterBaseClass or similar to provide the functionality you need. You can make this change quickly on all your TableAdapters by doing a 'Find in Files' and replace on your DataSets' .xsd files.

Instead of BFree's method on the caller with signature:

private void ChangeTimeout(Component component, int timeout)

you can then create a method on the callee TableAdapter's base class with signature:

public void ChangeTimeout(int timeout)

I have tried both the options and giving some issues On 1st Answer which namespace have to be imported/used for CommandCollection object on 2ns Answer adapter.SelectCommand is returning null value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!