Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Skip header reading from CSV file in C#

Posted on 2011-03-16
6
Medium Priority
?
4,747 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

963 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