0

First time using the csvReader - note it requires a custom class that defines the Headers found in the CSV file.

class DataRecord
{
    //Should have properties which correspond to the Column Names in the file 
    public String Amount { get; set; }
    public String InvoiceDate { get; set; }......
}

The example given then uses the class such:-

            using (var sr = new StreamReader(@"C:\\Data\\Invoices.csv"))
        {
            var reader = new CsvReader(sr);

            //CSVReader will now read the whole file into an enumerable
            IEnumerable<DataRecord> records = reader.GetRecords<DataRecord>();

            //First 5 records in CSV file will be printed to the Output Window
            foreach (DataRecord record in records.Take(5)) 
            {
                Debug.Print("{0} {1}, {2}", record.Amount, record.InvoiceDate, ....);
            }

Two questions :- 1. The app will be loading in files with differing headers so I need to be able to update this class on the fly - is this possible & how? (I am able to extract the headers from the CSV file.)

  1. CSV file is potentially multi millions of rows (gb size) so is this the best / most efficient way of importing the file.

Destination is a SQLite DB - debug line is used as example.

Thanks

James Budd
  • 71
  • 11
  • I have used linq2csv with great success. Not sure how it scales to GB of data though. It's probably best to break it into chunks.http://www.codeproject.com/Articles/25133/LINQ-to-CSV-library – Ray Suelzer Mar 15 '15 at 16:47
  • It allows you to define the headers on the fly too which is nice. – Ray Suelzer Mar 15 '15 at 16:48
  • Looks good & like the docs - thanks for highlighting as I hadn't seen this option. – James Budd Mar 15 '15 at 17:11

1 Answers1

0
  1. The app will be loading in files with differing headers so I need to be able to update this class on the fly - is this possible & how?

Although it is definetely possible with reflecion or third part libraries, creating an object for a row will be inefficient for such a big files. Moreover, using C# for such a scenario is a bad idea (unless you have some business data transformation). I would consider something like this, or perhaps a SSIS package.

Community
  • 1
  • 1
Alex Sikilinda
  • 2,928
  • 18
  • 34
  • Chalk it up to IT illiterate end users :( I have to import large financial datasets, perform statistical routines / data aggregation / keyword searches / etc. against the data & then churn out a report. Have tried various methods but am tired of hand holding & though making it in app form will hopefully make it pretty user proof (if I can code it correctly!!) – James Budd Mar 15 '15 at 17:46
  • You can "connect" to csv file using OleDbConnection and then just query it with regular SQL statements. See [this answer](http://stackoverflow.com/questions/6813607/parsing-csv-using-oledb-using-c-sharp). I hope it will help – Alex Sikilinda Mar 15 '15 at 18:00