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. Asp.Net MVC convert View to Word Document
  2. Limiting LINQ String Field Lengths
  3. Crystal Report Challenges
  4. Sharepoint Web Part Grouping

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

  • Return of the daily sites
  • Collaborating with Government
  • Ya-bing-ooo!! Yahoo search is dead long live the Bing.
  • Social Media – Good For Some
  • 10 Tips for user acceptance testing of web applications
  • Popular Content Management Systems
  • ASP.NET MVC [HandleError] and logging
  • VMWare hard lessons

Twitter

  • Collaborating with Government: Revium is actively involved in the Federal Government’s R&D program for tax concess... http://bit.ly/9D7Vc6 1 week ago
  • 45 Free Applications For Web Designers & Developers http://www.noupe.com/tools/45-free-applications-for-designers-and-developers.html 1 week ago
  • In most watch advertisements the time displayed on the timepiece is 10:10 because then the arms frame the brand of the watch. 1 week ago
  • More updates...

Revium Logo

  • Home
  • About
  • Expertise
  • Showcase
  • Contact

  • news
  • blog
  • sandbox
  • twitter
  • facebook
  • rss
Bookmark and Share

We are Revium, hear us roar!

The news.

31 Aug

Collaborating with Government

Revium is actively involved in the Federal Government’s R&D program for tax concessions to develop our in-house products that we are looking to take…

Continue reading
View archive

The blog.

01 Sep

Return of the daily sites

Years ago I used to have a morning routine of checking a select few websites I came across. For one reason or another I…

Continue reading
View archive

The sandbox.

20 Aug

Ya-bing-ooo!! Yahoo search is dead long live the Bing.

Ya-bing-oo!! Yahoo search is dead long live the Bing In July 2010 Yahoo had started testing Bing powered search they routed 25% of their search…

Continue reading
View archive

eNewsletter.

 

© Copyright 2010. All Rights Reserved.

Revium Pty Ltd

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

Find us: web development, seo

Disclaimer and privacy Revium Pty Ltd

10 Harvey Street
Richmond, Victoria, 3121 Australia
View map

Logo Partner MicrosoftLogo Partner KenticoLogo Partner IiaLogo Partner Iia RibLogo Partner Acs