2011-02-17 47 views
2

我必须检查我的datarow中的第一个单元格是否是日期时间对象。我正在为它做下面的事情。请让我知道是否有更好的方法来做到这一点?有没有更好的方法来检查数据通道中的类型?

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

您可以使用

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

is关键字检查左侧的类型,看它是否与在右侧给出的类型兼容侧。

相关问题