Solved

Skip header reading from CSV file in C#

Posted on 2011-03-16
6
4,205 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Telerik RadEditor Control Save 8 39
ASP.NET data base connection 35 95
asp.net, radiobuttonlist, c# 3 69
Cannot interpret token '"' at position 10. 3 25
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use and create keystrokes 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.

751 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