CSV parsing – the easy way!

by Mark Wiseman on March 19th, 2009 | Posted in Revium Sandbox | Read the comments

Every now and then we have a client that needs to, on a regular basis, import data from one system to another and a lot of the time we go for the good old csv file. In the past i have always just written a simple function that reads each line of the file, splits it into cells and import it into the database. But this simple code never fully handles csv’s correctly. I kept thinking “there has to be a simpler way“.

So i got thinking…

Csv files are just like data tables, data tables can be queried… surely there must be a way to just treat this file as a datasource. After a bit of playing around and reading i can up with the following solution.

It requires that the csv data be saved to a file and then we can just use an OleDataAdapter to perform almost any simple SQL statment against it!

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;

public static DataTable ReadCSVFileIntoDataTable(string pFilePath)
{
    string fullPath = Path.GetFullPath(pFilePath);
    string file = Path.GetFileName(fullPath);
    string dir = Path.GetDirectoryName(fullPath);

    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
        + "Data Source=\"" + dir + "\\\";"
        + "Extended Properties=\"text;HDR=No;FMT=Delimited\"";

    string query = "SELECT * FROM " + file;

    DataTable dt = new System.Data.DataTable();
    OleDbDataAdapter da = new OleDbDataAdapter(query, connString);

    try
    {
        da.Fill(dt );
    }
    catch (InvalidOperationException /*e*/)
    { }

    dAdapter.Dispose();

    return dt;
    }

Related posts:

  1. Parsing CSV files in .NET
  2. Creating a generic settings repository in C#
  3. Kentico, Smart Search and filtering attachments
  4. Asp.Net MVC convert View to Word Document
  5. Limiting LINQ String Field Lengths

Tags: parse, split

« Asp.Net MVC convert View to Word Document

ASP.NET Button – prevent double clicks »

One Response to “CSV parsing – the easy way!”

  1. Evgeny Petrov says:
    March 26, 2009 at 1:29 pm

    This is a very simple and effective way to deal not only with csv files, but e.g. with MS Excel documents as well – we can read and update them using OLE DB. There are lots of 3rd party OLE DB providers out there which may be used for the purpose of reading and writing to formatted files from c#.

Leave a Reply

Click here to cancel reply.

Recent Articles

  • ISAF Sailing World Cup
  • Revium Supports the Prostate Cancer Foundation of Australia
  • Kentico FAQ Module
  • Advanced Visitor Tracking in Analytics
  • Kentico, Smart Search and filtering attachments
  • Enhancing JIRA’s Issue Navigator
  • Mobile Browsing
  • Revium help win gold for Australia

Twitter

  • All things Gold! http://t.co/9DkkjmAr 2012-09-13
  • Mat Belcher - our favourite London Gold Medalist dropped into the office to say thank you. http://t.co/TxHbe2y6 2012-09-13
  • You beauty - http://t.co/1kbcBZwg #london2012 @belcherpage2012 2012-08-10
  • More updates...

Revium Logo

  • Home
  • About
  • Expertise
  • Showcase
  • Contact

  • news
  • blog
  • sandbox
  • twitter
  • rss
  • visit our facebook page

We are Revium, hear us roar!

The news.

26 Feb

Revium Supports the Prostate Cancer Foundation of Australia

We here at Revium are proud to say that we are supporting the Prostate Cancer Foundation of Australia, this month we have been able…

Continue reading
View archive

The blog.

11 Apr

ISAF Sailing World Cup

Mat Belcher and his team have been successful in taking out round 3 of the ISAF Sailing World Cup in Palma de Mallorca a…

Continue reading
View archive

The sandbox.

20 Dec

Kentico, Smart Search and filtering attachments

We had a scenario recently where we had 2 indexes set up in Kentico to search different folders within our site. Everything worked as…

Continue reading
View archive

eNewsletter.

 

© Copyright 2013. All Rights Reserved.

Revium Pty Ltd

info@revium.com.au Work +61 3 9429 2000

10 Harvey Street
Richmond, Victoria, 3121 Australia
View map

Disclaimer and privacy Revium Pty Ltd

Find us: web development, seo

 
Partner logos