如何跳过不良记录
我正在使用 文件助手 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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
您可以使用
BeforeReadRecord
事件来解析记录行并为您需要跳过的任何记录设置skipThisRecord = True
。例如:然后事件本身:
在上面的示例中,任何以空格或“-”开头的记录都将被跳过,但您可以应用您需要的任何逻辑。您可以使用
e.RecordLine.Split(',')
将当前行拆分为列值数组,然后使用DateTime.TryParse()
确定是否日期字符串有效。You can use the
BeforeReadRecord
event to parse the record line and setskipThisRecord = True
for any records you need to skip. For instance:Then the event itself:
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 useDateTime.TryParse()
to determine if the date string is valid.