SIGN UP MEMBER LOGIN:    
ARTICLE

Read CSV file using C#

Posted by Lasantha PW Articles | C# Language January 23, 2011
I'm going to explain how extract data from csv file using c#.
Reader Level:

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.

        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.
       
        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.
 
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...

Login to add your contents and source code to this article
share this article :
post comment
 

I'm not sure when this was written or if you've gotten this notice before but I have code similar to this (using JET instead of ODBC) and have run into a serious drawback. I have a field that is numbers, but I use a CSV format instead of Excel because I NEED to keep the leading zeros. when I use this method it reads that field as a double and drops the leading zeros.

Posted by Thomas Jul 14, 2011

b bn

Posted by Girish Sarbhukan Jan 25, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor