Mahesh Bodepudi

Mahesh Bodepudi

  • NA
  • 90
  • 56.9k

Loading C# datatable very fast

Jun 6 2016 3:35 AM
Hi,
In my windows forms application I have a task to read 1 lakh records from a notepad text file and then need to modify he data
by reading the data in a datatable and adding some new columns and doing some manipulations and finally when click on export button
it should generate an excel file.
For reading all the text file(1 lakh records) in datatable the process is taking 15 to 20 minutes of time.So if there are 6 text
files in a particular folder to export the text files sheet wise it is taking almost 2 hours.
Could you please any one explain how to overcome this one by exporting the data into an excel file in less
thsn 10 min.
Here this particular block of code is executing almost 15 minutes.
//In the below for loop we are reading the values from the text file by splitting with comma and adding
//to the datatable.
for (int i = 1; i < lines.Count(); i++)
{
if (i < lines.Count() - 1 && lines[i] == lines[i + 1]) continue;//to remove duplicate entries 26-May-16
dr = dt.NewRow();
values = lines[i].Split(new char[] { ',' });
for (int j = 0; j < values.Count() && j < columns.Count(); j++)
dr[j] = values[j];
dt.Rows.Add(dr);
DateTime = Convert.ToDateTime(dt.Rows[dt.Rows.Count - 1][Constants.Date].ToString());
// strDateTime = DateTime.ToString("dd/MM/yyyy").Split('/');
tempDate = DateTime.ToShortDateString();
tempTime = DateTime.ToLongTimeString();
FunName = dt.Rows[dt.Rows.Count - 1][Constants.FuncName].ToString();
dt.Rows[dt.Rows.Count - 1][Constants.Index] = dt.Rows.Count;
dt.Rows[dt.Rows.Count - 1][Constants.Time] = tempTime;
dt.Rows[dt.Rows.Count - 1][Constants.Date] = tempDate;
dt.AcceptChanges();
}

Answers (2)