Solved

Skip header reading from CSV file in C#

Posted on 2011-03-16
6
3,924 Views
Last Modified: 2013-12-17
I  would like to modify this to to skip the first row or line or header... of the csv file I'm reading and creating a table.

Please help

    private DataTable GetDataTable(string textFile)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name");
        dt.Columns.Add("age");
        dt.Columns.Add("code");
 
        if (File.Exists(textFile))
        {
            using (StreamReader sr = new StreamReader(textFile))
               
            {


                while (!sr.EndOfStream)
                 
                {

                    string value = sr.ReadLine();
                    //string contents = sr.ReadLine();
 
                    if (!string.IsNullOrEmpty(value))
                    {
                        string[] values = value.Split(new char[] { ';' });
                       

                    if (values.Length > -1)

                           
                        {
                         
                            DataRow row = dt.NewRow();
                            row["name"] = values[0];
                            row["age"] = values[1];
                            row["code"] = values[2];
                            dt.Rows.Add(row);
                        }
                    }
                }
            }
        }
 
        return dt;
    }
0
Comment
Question by:portalvale
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35150203
Hi,

Use following connection string to open CSV file.

System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";" + "Extended Properties='text;FMT=Delimited(;);HDR=YES'");

Reset HDR to No and enjoy.
0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35150256
Use this code to select from CSV:

 
string path = "c:\testInput\Data\Combisep\2004\Processed\OM0001122-GOP";
string fileToQuery = "OM0001122-GOP_Combisep_outFile.csv"
string sql = "SELECT * FROM " + fileToQuery;

Open in new window

0
 
LVL 10

Accepted Solution

by:
ALaRiva earned 500 total points
ID: 35150440
Instead of changing everything around, as asimnazir123 has advised incorrectly.

Just add this:
sr.ReadLine();


Right before the While statement loop.

That'll read the first line and do nothing, which basically puts you at line 2 when you start the while-loop which does what you need.

hth

- Anthony
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35150540
With my approach you can get a DataSet of delimited file and play with it much easily instead of reading line by line and then facing problems in reading individuals row-cell values.

Option is yours!
0
 

Author Closing Comment

by:portalvale
ID: 35150553
Awesome... thx
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 35150561
He was already done with his code, he just wanted to skip the header row.

Makes ZERO sense to offer someone to rewrite the entire thing with a different approach just to fix one thing that had nothing to do with the flexibility it gained.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now