Made in USA: Enterprise Application Services

Ringing PhoneCall Today!817-210-4042

CSV Integration into .Net Application

CSV Integration into .Net Application

October 3, 2013

My brother and I had a sure fire way of getting out of things as children.

Whenever our dad would assigned us a task, we would simply do it the “wrong way”. He would take over and we could continue playing HeMan or Inspector Gadget or whatever game we wanted to play instead of mowing the lawn.

As I’ve gotten older, I realized that there was a reason my father wanted us to do things his way. It got the work done in a timely manner. If we had just stopped screwing around, we could’ve helped out our dad and still had time to reenact awesome 80s/90s cartoons.

CSV Integration...or watch Darkwing Duck?
Now, whenever I’m given a task to complete, I realize that there is already someone, somewhere who has come across a similar problem and created an eloquent solution that I can adapt to my situation.

I was recently given the task of taking a client’s CSV (comma separated value) file and importing it into a database through a .NET application. I had done this before and knew that I could do it based on the index of the column (i.e. column 1 matched up with the property Name).

But this seemed really inefficient to me, what happened if the columns changed, or the order was rearranged… the whole method would have to be rewritten and retested.

So, I set out in an effort to find a way to import a CSV file with column recognition.

And there I found CsvHelper. This is a C# library from developer Josh Close on github that allows a simple way for you to take a CSV file, read it and map it to column headings to import it into a pre-defined class in your project and subsequently your database.

It’s not perfect (nothing in programming ever is).

If a column doesn’t exist or if the column header is capitalized in an odd way, or not at all, the mapping won’t work.

But for the most part it is a unique and innovative tool. And reduced my time spent from a full day (including documentation so future developers could tell what column mapped to what property) to a couple of hours.

Now, I have time to watch Care Bears and Darkwing Duck with my kids instead of working late into the night.

Related Posts