3

File Helpers 2.9.9を使用していますが、クラッシュするだけでなく、不良レコードをスキップするにはどうすればよいですか?

object[] transactions = engine.ReadStream(textReader); // will crash if one record fails.

DateTimeにも問題があります。設定した形式を使用して「2011年12月22日」を変換できない理由がわかりません。

Error Converting '"12/22/2011"' to type: 'DateTime'.  does not match any of the given formats: 'MM/dd/yyyy', 'MM/d/yyyy', 'M/d/yyyy'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: FileHelpers.ConvertException: Error Converting '"12/22/2011"' to type: 'DateTime'.  does not match any of the given formats: 'MM/dd/yyyy', 'MM/d/yyyy', 'M/d/yyyy'
4

2 に答える 2

4

1)[編集] -私は間違っていました。engine.ErrorManager.ErrorModeをSaveAndContinueに設定できます-例を参照してください@ http://www.filehelpers.com/example_errorhandling.html

2)二重引用符付きの文字列を含む一重引用符に基づくと、問題はFieldQuoted属性を指定する必要があることです。http://www.filehelpers.com/attributes.htmlを参照してください。

于 2012-01-15T03:40:48.600 に答える
3

BeforeReadRecordイベントを使用してレコード行を解析し、skipThisRecord = Trueスキップする必要のあるレコードを設定できます。例えば:

FileHelperEngine engine = new FileHelperEngine(typeof(Orders)); 
// set the event here
engine.BeforeReadRecord += new BeforeReadRecordHandler(BeforeEvent); 

次に、イベント自体:

private void BeforeEvent(EngineBase engine, BeforeReadRecordEventArgs e)
{
    // skip any bad lines
    if (e.RecordLine.StartsWith(" ") || e.RecordLine.StartsWith("-"))
        e.SkipThisRecord = true;
}

上記の例では、スペースまたは「-」で始まるレコードはスキップされますが、必要なロジックを適用できます。を使用e.RecordLine.Split(',')して現在の行を列値の配列に分割し、を使用DateTime.TryParse()して日付文字列が有効かどうかを判断できます。

于 2012-01-16T10:37:42.283 に答える