Click here to Skip to main content
15,867,308 members
Articles / General Programming / File

Read CSV file using C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
11 Oct 2013CPOL 45.2K   6  
I'm going to expalin how extract data from csv file using c#.First you have to declare two string variables and their properies for store directory

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

I'm going to explain how extract data from csv file using c#.

First you have to declare two string variables and their properties for store directory and filename of csv file which you want to extract data.

C#
private string dirCSV;
private string fileNevCSV;

public string FileNevCSV
{
 get{return fileNevCSV;}
 set{fileNevCSV=value;}
}

public string dirCSV
{
 get{return dirCSV;}
 set{dirCSV=value;}
} 
In the second step connect to the data source and fill it to the dataset.
C#
public DataSet loadCVS(int noofrows)
        {
            DataSet ds = new DataSet();
            try
            {
                // Creates and opens an ODBC connection
                string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
                string sql_select;
                OdbcConnection conn;
                conn = new OdbcConnection(strConnString.Trim());
                conn.Open();
                //Creates the select command text
                if (noofrows == -1)
                {
                    sql_select = "select * from [" + this.FileNevCSV.Trim() + "]";
                }
                else
                {
                    sql_select = "select top " + noofrows + " * from [" + this.FileNevCSV.Trim() + "]";
                }
                //Creates the data adapter
                OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);
                //Fills dataset with the records from CSV file
                obj_oledb_da.Fill(ds, "csv");
                //closes the connection
                conn.Close();
            }
            catch (Exception e) //Error
            {
            }
            return ds;
        } 

In the third step extract data to DataTable from generated DataSet.

C#
this.dirCSV = "file path";
this.fileNevCSV ="file name";

DataSet ds = loadCVS(-1);
DataTable table = ds.Tables[0];

foreach (DataRow row in table.Rows)
   {
     //iterate through the DataTable.
   }

Thanks.
Happy coding..

This article was originally posted at http://wiki.asp.net/page.aspx/1570/read-csv-file-using-c

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --