如何验证存储在C#之前.csv文件?文件、csv

2023-09-07 08:56:46 作者:淡笑看过尘世。

我有一些的.csv文件,我存储在数据库中之前我解析。

I have some .csv files which I am parsing before storing in database.

我想使应用程序更为健壮,并且在该.csv文件之前保存在数据库中执行验证。

I would like to make application more robust, and perform validation upon the .csv files before save in the database.

所以,我问你们,如果你有一些很好的链接,或code的例子,图案,或建议如何做到这一点?

So I am asking you guys if you have some good links, or code examples, patterns, or advice on how to do this?

我会在下面贴我的.csv文件的一个例子。在.csv文件中的不同的数据字段由制表符分隔。每个数据新行是一个新行。

I will paste an example of my .csv file below. The different data fields in the .csv file are separated by tabs. Each new row of data is on a new line.

我一直在思考一些有关的事情,我应该验证反对,并提出了下面的列表中(我很开放的其他建议,如果你有任何事情,你认为应该加入到列表中?)

I have been thinking a little about the things I should validate against and came up with the list below (I am very open for other suggestions, in case you have anything which you think should be added to the list?)

Correct file encoding.
That file is not empty.
Correct number of lines/columns.
correct number/text/date formats.
correct number ranges.

这是我的.csv文件看起来像(文件两行,一行数据由制表符分隔)。

This is how my .csv file looks like (file with two lines, data on one line is separated by tabs).

4523424 A123456 GT-P1000    mobile phone    Samsung XSD1234 135354191325234
345353  A134211 A8181   mobile phome    HTC S4112-ad3   111911911932343

该字符串重新$ P $以上psentation是这样的:

The string representation of above looks like:

"4523424\tA123456\tGT-P1000\tmobile phone\tSamsung\tXSD1234\t135354191325234\r

\n345353\tA134211\tA8181\tmobile phome\tHTC\tS4112-ad3\t111911911932343\r\n"

所以,你有什么好的设计,链接,图案,code实例等就如何做到这一点在C#?

So do you have any good design, links, patterns, code examples, etc. on how to do this in C#?

推荐答案

adrianm和Nipun Ambastha

adrianm and Nipun Ambastha

感谢您回应我的问题。

我写一个解决方案来验证我的.csv文件我解决我的问题。

I solved my problem by writing a solution to validate my .csv file myself.

这是非常有可能一个更优雅的解决方案可以通过利用adrianm的code制成的,但我没有这样做,但我鼓励给adrianm的codeA的样子。

It's quite possible a more elegant solution could be made by making use of adrianm's code, but I didn't do that, but I am encouraging to give adrianm's code a look.

我验证下面的列表中。

空文件 新的FileInfo(dto.AbsoluteFileName).Length == 0

Empty file new FileInfo(dto.AbsoluteFileName).Length == 0

错误格式的文件行。     字符串[]项目= line.Split('\ t'); 如果(items.Count()== 20)

Wrong formatting of file lines. string[] items = line.Split('\t'); if (items.Count() == 20)

错误的数据类型相符领域。 INT编号;     布尔ISNUMBER = int.TryParse(dataRow.ItemArray [0]的ToString(),出号);

Wrong datatype in line fields. int number; bool isNumber = int.TryParse(dataRow.ItemArray[0].ToString(), out number);

缺少必需的行字段。 如果(dataRow.ItemArray [4]的ToString()长度和酰胺。1)

Missing required line fields. if (dataRow.ItemArray[4].ToString().Length < 1)

要工作,通过我根据.csv文件的内容,在这个code例如我的code:

To work through the contents of the .csv file I based my code on this code example:

http://bytes.com/topic/ C-锐/答案/ 256797-读制表符分隔文件