2011-04-14 1 views
0

테이블 어댑터에서 데이터를 검색하는 데 걸리는 시간을 늘리고 싶습니다. 어떻게 설정합니까? 나는 이것을 사용하여 시도 :SelectCommandTimeout 속성에서 테이블 어댑터 문제가 발생했습니다. C#

http://www.codeproject.com/KB/database/TableAdaptrCommandTimeout.aspx

그러나, _commandCollection.Length 따라서 null로 설정됩니다 내가 CommandTimeout이

어떤 아이디어를 설정할 수 없습니다 무엇입니까?

답변

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

편집 : 확장 메서드가 멋진!

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); 
0

을이 작품을 올바르게.

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; 
     } 
    } 
    } 
} 
유일한 생각은 원래 CodeProject의에서 솔루션을 코드의이 행을 추가