Click here to Skip to main content
15,850,685 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how do i read a text csv file and store in table using

plzz help me ..

well the data is large

Share this answer
check this link's. Remember before you process you need to upload the file to web server.

Read CSV file using C#[^][^]
Share this answer
                //Step -1) Read the file from local  and save to Datatable 

                DataTable dt = new DataTable();
                string line = null;
                int i = 0;

                //Pass the file  
                using (StreamReader sr = File.OpenText(Server.MapPath("~/temp/table1.csv")))
                    //Read line by line from CSV file
                    //Save the line to  string  and check for null
                    while ((line = sr.ReadLine()) != null)
                        //If not null then split with "," comma
                        string[] data = line.Split(',');
                        if (data.Length > 0)
                            //if Array length is >0  and at initial value first add column to datatable
                            if (i == 0)
                                foreach (var item in data)
                                    dt.Columns.Add(new DataColumn());
                            //Create DataRow  instance 
                            DataRow row = dt.NewRow();
                            //save all array to DataRow ItemArray collection
                            row.ItemArray = data;


//Step-2 ) If DataTable is not null then insert this Datatable to Database using BulkCopy
          //After saving csv file to DataTable  insert data using SqlBulkCopy
                if (dt != null && dt.Rows.Count > 0)

                    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlBulkCopy"].ConnectionString))
                        using (SqlBulkCopy copy = new SqlBulkCopy(cn))

                            copy.ColumnMappings.Add(0, 1);
                            copy.ColumnMappings.Add(1, 2);
                            copy.ColumnMappings.Add(2, 3);
                            copy.ColumnMappings.Add(3, 4);
                            copy.ColumnMappings.Add(4, 5);

                            copy.DestinationTableName = "tablename";


Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900