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

3
  • 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.codeproject.com/Articles/25133/LINQ-to-CSV-library Commented Mar 15, 2015 at 16:47
  • It allows you to define the headers on the fly too which is nice. Commented Mar 15, 2015 at 16:48
  • Looks good & like the docs - thanks for highlighting as I hadn't seen this option.
    – James Budd
    Commented Mar 15, 2015 at 17:11

1 Answer 1

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.

2
  • 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
    Commented Mar 15, 2015 at 17:46
  • You can "connect" to csv file using OleDbConnection and then just query it with regular SQL statements. See this answer. I hope it will help Commented Mar 15, 2015 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.