如何跳过不良记录

发布于 2024-12-27 02:20:51 字数 840 浏览 4 评论 0 原文

我正在使用 文件助手 2.9.9 ,我想知道如何让它跳过坏记录而不是只是崩溃?

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

我在 DateTime 方面也遇到了麻烦。我不明白为什么它无法使用我设置的格式转换“12/22/2011”。

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'

I am using File Helpers 2.9.9 and I am wondering how do I get it skip over bad records instead of it just crashing?

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

I am also having trouble with the DateTime.I can't see why it can't convert "12/22/2011" using the formats I have set.

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2025-01-03 02:20:51

1) [编辑] - 我错了,你可以将 engine.ErrorManager.ErrorMode 设置为 SaveAndContinue - 请参阅示例 @ http://www.filehelpers.com/example_errorhandling.html

2)基于包含带双引号的字符串的单引号,我想说问题是您需要提供FieldQuoted 属性 - 请参阅 http://www.filehelpers.com/attributes.html

1) [EDIT] - I was wrong, you can set engine.ErrorManager.ErrorMode to SaveAndContinue - see the examples @ http://www.filehelpers.com/example_errorhandling.html

2) based on the single quotes containing a string with double-quotes, I would say the problem is that you need to provide the FieldQuoted attribute - see http://www.filehelpers.com/attributes.html

笑脸一如从前 2025-01-03 02:20:51

您可以使用 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() 确定是否日期字符串有效。

You can use the BeforeReadRecord event to parse the record line and set skipThisRecord = True for any records you need to skip. For instance:

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

Then the event itself:

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

In the example above any record which begins with a space or a '-' will be skipped, but you can apply any logic you need. You could use e.RecordLine.Split(',') to split the current line into an array of column values and then use DateTime.TryParse() to determine if the date string is valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文