0

We are receiving a CSV file as Stream. Now we are creating the data table from the stream as below

while (!reader.EndOfStream)
        {
            try
            {
                string[] rows = reader.ReadLine().Split(',');
                DataRow dr = dt.NewRow();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    dr[i] = rows[i].Replace("\"", "");
                }
                dt.Rows.Add(dr);
            }
            catch (Exception ex)
            {
                continue;
            }
        }

It's taking some time to create the Data Table as CSV in Stream contains lot of records. We want to have the best solution to do the same. Can you please share, If you know any other better solution than this.

Midhun Murali
  • 2,089
  • 6
  • 28
  • 49
  • refer this; http://stackoverflow.com/questions/1050112/how-to-read-a-csv-file-into-a-net-datatable?lq=1 – Irshad Mar 03 '16 at 11:04
  • In above post a answer suggested to use Microsoft.Jet.OLEDB.4.0 provider with your CSV file. Here is the answer; http://stackoverflow.com/a/1050278/2130976 – Irshad Mar 03 '16 at 11:07

1 Answers1

0

Catching exception can slow down your process. Try to handle all cases in the code.

  catch (Exception ex)
  {
            continue;
  }
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36