Solved

Skip header reading from CSV file in C#

Posted on 2011-03-16
6
4,353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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.

623 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