Solved

Skip header reading from CSV file in C#

Posted on 2011-03-16
6
3,844 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Expert Comment

by:Asim Nazir
Comment Utility
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
Comment Utility
Awesome... thx
0
 
LVL 10

Expert Comment

by:ALaRiva
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

771 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

15 Experts available now in Live!

Get 1:1 Help Now