2011-02-17 2 views
2

내 datarow의 첫 번째 셀이 datetime 개체인지 확인해야합니다. 다음과 같이 처리하고 있습니다. 더 나은 방법이 있는지 알려 주시겠습니까?데이터 로우의 유형을 확인하는 더 좋은 방법이 있습니까?

public bool ShouldProcess(DataRow theRow) 
     { 
      try 
      {     
       Convert.ToDateTime(theRow[0]); 
      } 
      catch (Exception) 
      { 
       return false; 
      } 

      return true; 
     } 

감사합니다, -M

답변

1

if (theRow[0] is DateTime)을 사용해 보셨습니까?

1

기억 try/catch

DateTime outDate = null; 
DateTime.TryParse(theRow[0], out outDate); 
if(outDate != DateTime.MinDate) 
{ 
//Successfully converted 
} 
0

이 오히려

DateTime.TryParse Method 또는 DateTime.TryParseExact Method

를 사용하여 살펴을 배치 할 필요가 없습니다 이들 M ethod는 bool 값을 반환하므로 bool 값을 반환하는 데 사용할 수 있습니다.

뭔가

처럼 는
DateTime dateVal; 
return DateTime.TryParse(theRow[0], out dateVal); 
1

당신은

if(theRow[0] is DateTime) 
    return true; 
else 
    return false 

is 키워드는 오른쪽에 주어진 유형과 호환 여부를 확인하기 위해 왼쪽의 유형을 확인 사용할 수 있습니다 측면.

관련 문제