我想增加從我的tableadapter檢索數據的時間。我如何設置它?我嘗試使用這樣的:tableadapters問題與SelectCommandTimeout屬性c#
http://www.codeproject.com/KB/database/TableAdaptrCommandTimeout.aspx
然而,_commandCollection.Length設定因此爲null我無法設置的CommandTimeout
任何想法?
我想增加從我的tableadapter檢索數據的時間。我如何設置它?我嘗試使用這樣的:tableadapters問題與SelectCommandTimeout屬性c#
http://www.codeproject.com/KB/database/TableAdaptrCommandTimeout.aspx
然而,_commandCollection.Length設定因此爲null我無法設置的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);
編輯:擴展方法是爽!這項工作正確
AccessoryTableAdapter ata = new AccessoryTableAdapter();
ata.Adapter.setAdapterTimeout(120);
在我的情況:
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;
}
}
然後調用它。 唯一認爲這是在原來的CodeProject上添加此行的代碼解決方案:
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;
}
}
}
}