How to validate a .csv file before storage in C#?

后端 未结 4 1693
迷失自我
迷失自我 2021-01-14 07:55

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

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

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 08:18

    adrianm and Nipun Ambastha

    Thank you for your response to my question.

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

    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.

    I am validating the list below.

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

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

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

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

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

    http://bytes.com/topic/c-sharp/answers/256797-reading-tab-delimited-file

提交回复
热议问题