2011-04-14 33 views

回答

3

您必須在您的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); 
0

在我的情況:

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