简体   繁体   English

使用schema.ini作为验证器将CSV导入SQL

[英]Import CSV to SQL using schema.ini as validator

I'm using schema.ini to validate the data types/columns in my CSV file before loading into SQL. 在加载到SQL之前,我正在使用schema.ini验证CSV文件中的数据类型/列。 If there is a datatype mismatch in a row, it will still import the row but leaves that particular mismatch cell blank. 如果一行中的数据类型不匹配,它仍将导入该行,但会将特定的不匹配单元格留空。 Is there a way in which I can stop user from importing the CSV file if there is any issues and/or provide a error report (ie which row has problems). 如果有任何问题和/或提供错误报告(即哪一行有问题),有什么方法可以阻止用户导入CSV文件。

The best approach is to check the file for any mismatch; 最好的方法是检查文件是否不匹配。 but in the case of a large file, then this is not feasible. 但是对于大文件,则不可行。 You might need to load it first the check the loaded data in the table for the mismatch. 您可能需要先加载它,然后检查表中已加载的数据是否不匹配。 This is much faster than checking the file (You can use a simple T-SQL script to check for nulls in the table). 这比检查文件快得多(您可以使用简单的T-SQL脚本检查表中的null)。 IF mismatches are found, the user can then be notified and the table can then be cleared. 如果发现不匹配,则可以通知用户,然后可以清除该表。

have a look at he FileHelpers library: http://www.filehelpers.com/ 看看他的FileHelpers库: http : //www.filehelpers.com/

This is a very powerful library to do all kinds of imports, including csv and they also have a pretty neat error handling part 这是一个非常强大的库,可以执行各种导入,包括csv,它们也有一个非常简洁的错误处理部分

Using the Differents Error Modes The FileHelpers library has support for 3 kinds of error handling. 使用差异错误模式FileHelpers库支持3种错误处理。

In the standard mode you can catch the exceptions when something fail. 在标准模式下,您可以在出现故障时捕获异常。 This approach not is bad but you lose some info about the current record and you can't use the records array because is not asigned. 这种方法还不错,但是您会丢失一些有关当前记录的信息,并且由于未分配记录而无法使用records数组。

A more intelligent way is usign the ErrorMode.SaveAndContinue of the ErrorManager: 一种更智能的方法是使用ErrorManager的ErrorMode.SaveAndContinue:

Using the engine like this you have the good records in the records array and in the ErrorManager you have te records with errors and can do wherever you want. 使用这样的引擎,您可以在records数组中拥有良好的记录,在ErrorManager中,您可以拥有带有错误的记录,并且可以在任何地方进行操作。

Another option is to ignore the errors and continue how is showed in this example 另一个选择是忽略错误并继续执行本示例中的显示方式

1 engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue; 1 engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue; 2 3 records = engine.ReadFile(... Copy to Clipboard | View Plain | Print | ? engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue; 2 3条记录= engine.ReadFile(...复制到剪贴板|视图纯色|打印|?engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue;

records = engine.ReadFile(... In the records array you only have the good records. records = engine.ReadFile(...在records数组中,您只有良好的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM