2011-02-17 6 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 ethodsはbool値を返すので、それを使ってbool値を返すことができます。

あなたはisキーワードは、それが右側に指定された型と互換性があるかどうかを確認するために左側のタイプをチェックし

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

を使用することができます

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

のようなもの側。

関連する問題