0

CSVHelper and FileHelper is not an option

I have a .csv export that I need to check for consistency structured like the below

Reference,Date,EntryID

ABC123,08/09/2015,123

ABD234,08/09/2015,124

XYZ987,07/09/2015,125

QWE456,08/09/2016,126

I can use ReadLine or RealAllLines and .Split which give me entire rows/columns BUT I have need to select each row and then go through each attribute (separated by ',') for format checking

I am running into problems here. I can not single out each value in a row for this check. It is probably either something simple onto

 class Program
{
    static void Main(string[] args)
    {
        string csvFile = @"proof.csv";
        string[] lines = File.ReadAllLines(csvFile);


        var values = lines.Skip(1).Select(l => new { FirstRow = l.Split('\n').First(), Values = l.Split('\n').Select(v => int.Parse(v)) });
        foreach (var value in values)
        {
            Console.WriteLine(string.Format("{0}", value.FirstRow));
        }
    }
}

Or I am going down the wrong path, my searches relate to pulling specific rows or columns (as opposed to checking the individual values associated)

The sample of the data above has a highlighted example: The date is next year and I would like to be able to proof that value (just an example as it could be in either column where errors appear)

user001
  • 366
  • 1
  • 4
  • 21
  • Well if you are trying to get each cell value in an array you can use CSVHelper. http://joshclose.github.io/CsvHelper/ – Riki Sep 08 '15 at 10:17
  • Apologies, I have it at the start but realise that it may get lost in the text. I can not use cvsHelper or FileHelper. Don't have permissions – user001 Sep 08 '15 at 10:18
  • What are you trying to prove? Whether the csv contains specific values like the one you highlighted here or whether, as per your ex, the 2nd col is always of type DateTime, the 3rd is of type Number and so on? – Piyush Sep 08 '15 at 10:26
  • So, you're trying to find whether the last line of the CSV is italic? I kid, off course, but it's unclear *what* exactly you're trying to validate, and a generic solution will probably just be more confusing – Martijn Sep 08 '15 at 10:30
  • Also note that despite appearing simple, CSV is not a simple (or well-defined) format at all – Martijn Sep 08 '15 at 10:31
  • I will need to ensure that the 2nd column is a valid date and that the 3rd is a correct entry within a set range. The 1st column as a Varchar will be difficult but a minimum that I can do is ensure that it is the correct length – user001 Sep 08 '15 at 10:37

2 Answers2

2

I can not single out each value in a row

That's because you split on \n twice. The values within a row are separated by comma (,).

I'm not sure what all that LINQ is supposed to do, but it's as simple as this:

string[] lines = File.ReadAllLines(csvFile);

foreach (var line in lines.Skip(1))
{
    var values = line.Split(',');
    // access values[0], values[1] ...
}
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
0

Instead of reading it as text read it by OLEDB object, so data of CSV file will come in datatable and you do not need to spit it.

To Read the csv file you can use these objects of OLEDB

System.Data.OleDb.OleDbCommand 
System.Data.OleDb.OleDbDataAdapter
System.Data.OleDb.OleDbConnection

and

System.Data.DataTable
Neeraj Kumar Gupta
  • 2,045
  • 7
  • 26
  • 47