gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:58:35 25 4
gpt4 key购买 nike

我正在使用带有 C# 的 Visual Studio 2008。

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

感谢您的帮助。

最佳答案

通过一些小的修改,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;
}
}
}

要使用它,只需设置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;
}

用法:

this.FooTableAdapter.TableAdapterCommandTimeout(60);
this.FooTableAdapter.Fill(...);

这有点慢。如果在错误类型的对象上使用它,则有可能出错。 (据我所知,没有可以将其限制为的“TableAdapter”类。)

关于c# - 如何更改表适配器的命令超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1192171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com