如何更改表适配器的命令超时

cag*_*gin 17 c# command timeout dataset

我正在使用Visual Studio 2008和C#.

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

谢谢你的帮助.

Mit*_*man 11

通过一些小的修改,csl的想法很有效.

partial class FooTableAdapter
{
  /**
   * <summary>
   * Set timeout in seconds for Select statements.
   * </summary>
   */
  public int SelectCommandTimeout
  {
    set
    {
            for (int i = 0; i < this.CommandCollection.Length; i++)
                if (this.CommandCollection[i] != null)
                 this.CommandCollection[i].CommandTimeout = value;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

要使用它,只需设置this.FooTableAdapter.CommandTimeout = 60; 在this.FooTableAdapter.Fill()之前的某个地方;


如果需要更改许多表适配器的超时,可以创建一个通用扩展方法并让它使用反射来更改超时.

/// <summary>
/// Set the Select command timeout for a Table Adapter
/// </summary>
public static void TableAdapterCommandTimeout<T>(this T TableAdapter, int CommandTimeout) where T : global::System.ComponentModel.Component
{                
    foreach (var c in typeof(T).GetProperty("CommandCollection", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance).GetValue(TableAdapter, null) as System.Data.SqlClient.SqlCommand[])
        c.CommandTimeout = CommandTimeout;
}
Run Code Online (Sandbox Code Playgroud)

用法:

this.FooTableAdapter.TableAdapterCommandTimeout(60);
this.FooTableAdapter.Fill(...);
Run Code Online (Sandbox Code Playgroud)

这有点慢.如果在错误类型的对象上使用它,则可能会出错.(据我所知,没有"TableAdapter"类可以限制它.)


tim*_*imh 11

我今天对此问题进行了一些调查,并根据几个来源提出了以下解决方案.我们的想法是为表适配器创建一个基类继承,这会增加表适配器中所有命令的超时,而不必重写太多的现有代码.它必须使用反射,因为生成的表适配器不会继承任何有用的东西.如果你想删除我在构造函数中使用的东西并使用它,它会公开一个公共函数来改变超时.

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我在使用Mitchell Gilman的解决方案时遇到了几个问题,最终我可以解决该问题。

首先,我需要确保使用正确的名称空间。我花了一段时间才弄清楚,xsd数据集的Designer文件实际上包含两个名称空间,一个用于一般数据集,另一个用于表适配器。因此,首先要注意的是,应该使用表适配器的名称空间,而不是通常用于数据集的名称空间。

其次,在第一次使用超时命令时,命令收集可能不会总是初始化。要解决此问题,在这种情况下,我调用了InitCommandCollection命令。

所以我使用的调整后的解决方案是

namespace xxx.xxxTableAdapters

partial class FooTableAdapter
{
  /**
   * <summary>
   * Set timeout in seconds for Select statements.
   * </summary>
   */
  public int SelectCommandTimeout
  {
    set
    {
        if (this.CommandCollection == null)
                this.InitCommandCollection();

        for (int i = 0; i < this.CommandCollection.Length; i++)
            if (this.CommandCollection[i] != null)
             this.CommandCollection[i].CommandTimeout = value;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

希望对人们有所帮助!