tableapters与SelectCommandTimeout属性的问题
本文关键字:问题 属性 SelectCommandTimeout tableapters | 更新日期: 2023-09-27 17:50:05
我想增加从tableadapter检索数据的时间。我怎么设置它?我试着用这个:
http://www.codeproject.com/KB/database/TableAdaptrCommandTimeout.aspx然而,_commandCollection。长度被设置为空,因此我无法设置CommandTimeout
任何想法?
在设置超时之前,您必须调用tableAdapter上的GetData()方法,否则SelectCommand将不会初始化。
protected void setAdapterTimeout(SqlDataAdapter da, int timeOut = 120)
{
if (da.SelectCommand != null)
da.SelectCommand.CommandTimeout = timeOut;
}
然后像这样调用它:
//Replacing AccessoryTableAdapter with your table Adapter
AccessoryTableAdapter ata = new AccessoryTableAdapter();
setAdapterTimeout(ata.Adapter);
EDIT:扩展方法很酷!
public static class Extensions
{
public static void setAdapterTimeout(this SqlDataAdapter da, int timeOut = 120)
{
if (da.SelectCommand != null)
da.SelectCommand.CommandTimeout = timeOut;
if (da.InsertCommand != null)
da.InsertCommand.CommandTimeout = timeOut;
}
}
则调用:
AccessoryTableAdapter ata = new AccessoryTableAdapter();
ata.Adapter.setAdapterTimeout(120);
在我的情况下,这工作正确。唯一的想法是将这行代码添加到原始的代码项目解决方案:
if ((this._commandCollection == null)) this.InitCommandCollection();
因此,SelectCommandTimeout属性变成:
// Add this check before the assign step...
if ((this._commandCollection == null)) this.InitCommandCollection();
for (int i = 0; i < this._commandCollection.Length; i++)
{
if ((this._commandCollection[i] != null))
{
((System.Data.SqlClient.SqlCommand)
(this._commandCollection[i])).CommandTimeout = value;
}
}
}
}