如何更改表适配器的命令超时 [英] How can I change the table adapter's command timeout

查看:178
本文介绍了如何更改表适配器的命令超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2008中使用C#。

I'm using Visual Studio 2008 with C#.

我有一个.xsd文件,它有一个表适配器。我想改变表适配器的命令超时。

I have a .xsd file and it has a table adapter. I want to change the table adapter's command timeout.

感谢您的帮助。

推荐答案

我已经研究过此问题有点今天拿出基于几个来源以下解决方案。
的想法是为表适配器创建一个基类太继承这增加为表中的适配器的所有命令的超时而不必重写太多现有的代码。它使用反射,因为生成的表适配器不继承任何有用的东西。它公开了一个公共职能,如果你想删除我在构造函数中使用,并用它来改变超时。

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

这篇关于如何更改表适配器的命令超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆