全局控制 TableAdapter 命令超时

发布于 2024-07-24 04:50:55 字数 997 浏览 3 评论 0原文

我有一个带有 QueriesTableAdapter 的数据集。 为了控制 SqlCommand.CommandTimeout,我添加了一个名为 QueriesTableAdapter 的分部类,其中包含一个名为 ChangeTimeout 的公共方法。

partial class QueriesTableAdapter
{
    public void ChangeTimeout(int timeout)
    {
        foreach (System.Data.SqlClient.SqlCommand cmd in CommandCollection)
        {
            cmd.CommandTimeout = timeout;
        }
    }
}

对于我拥有的每个具有 QueriesTableAdapter 的数据集,我可以在执行之前设置 CommandTimeout。

using (NameSpace.DataSet.DataSetTableAdapters.QueriesTableAdapter ta =
new NameSpace.DataSet.DataSetTableAdapters.QueriesTableAdapter())
{
    ta.ChangeTimeout(3600);
    ta.DoSomething();
}

这在大多数情况下效果很好,因为“QueriesTableAdapter”是在数据集设计器中为您命名的。 我遇到的问题是唯一命名的 TableAdapter。 例如,如果我有一个名为 Person 的 DataTable 和一个名为 PersonTableAdapter 的 TableAdaper,则我必须以与编写 QueriesTableAdaper 类相同的方式编写 PersonTableAdapter 部分类。 我有数百个具有唯一 TableAdapter 名称的 DataTable。 我不想为每个创建一个部分类。 如何以全局方式获取分部类的基础 SqlCommand 对象?

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 called ChangeTimeout.

partial class QueriesTableAdapter
{
    public void ChangeTimeout(int timeout)
    {
        foreach (System.Data.SqlClient.SqlCommand cmd in CommandCollection)
        {
            cmd.CommandTimeout = timeout;
        }
    }
}

For every DataSet I have that has a QueriesTableAdapter, I can set the CommandTimeout prior to executing.

using (NameSpace.DataSet.DataSetTableAdapters.QueriesTableAdapter ta =
new NameSpace.DataSet.DataSetTableAdapters.QueriesTableAdapter())
{
    ta.ChangeTimeout(3600);
    ta.DoSomething();
}

This works well is most cases because the "QueriesTableAdapter" is named for you in the DataSet designer. The problem I'm running into is the TableAdapters that are uniquely named. For example, if I have a DataTable called Person and a TableAdaper called PersonTableAdapter, I have to write a PersonTableAdapter partial class in the same way I wrote the QueriesTableAdaper class. I have hundreds of DataTables with unique TableAdapter names. I don't want to create a partial class for each of those. How can I get to the underlying SqlCommand objects of a partial class in a global way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ㄖ落Θ余辉 2024-07-31 04:50:56

BFree 和 mark 的类似解决方案与反射配合得很好。 下面是我认为会产生更简洁代码的轻微改进。

您还可以更改 TableAdapter 在数据集设计器中使用的基类。 您可以将 TableAdapter 的基类更改为 MyTableAdapterBaseClass 或类似的基类,以提供您需要的功能。 您可以通过执行“在文件中查找”并替换数据集的 .xsd 文件,在所有 TableAdapter 上快速进行此更改。

而不是调用者上的 BFree 方法:

private void ChangeTimeout(Component component, int timeout)

然后,您可以在被调用者 TableAdapter 的基类上创建一个带有签名的方法,

public void ChangeTimeout(int timeout)

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)
请别遗忘我 2024-07-31 04:50:56

我已经尝试了这两个选项并提出了一些问题
在第一个答案中,必须为 CommandCollection 对象导入/使用哪个名称空间
在 2ns 应答适配器上。SelectCommand 返回空值

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

去了角落 2024-07-31 04:50:55

由于某种原因,我的适配器的 .selectcommand 为空,所以我最终不得不遍历 CommandCollection 对象,所以我想我应该根据上面的先前答案发布我的小更改。

包括:

using System.ComponentModel;
using System.Reflection;

代码:

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

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;            
        }
终止放荡 2024-07-31 04:50:55

所有生成的TableAdapter都继承自Component。 因此,您可以编写一个像这样的方法,使用反射来提取适配器属性:

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

然后您可以这样调用它:

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

我假设由于您使用的是类型化 DataSet,所以您仍然处于 .NET 2.0 中,这就是为什么我没有费心将其作为扩展方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文