I have developed a code in C# that copies data from csv file to a data table. The csv file contains 5 Million rows and I read the rows line by line to prevent memory issues. I wonder why I still get OutOfMemory Exception. I added breakPoints to make sure the right strings are copied to my variables and they are working correctly. Any ideas?
int first_row_flag = 0; //first row is column name and we dont need to import them
string temp;
foreach (var row in File.ReadLines(path3))
{
if (!string.IsNullOrEmpty(row))
{
int i = 0;
if (first_row_flag != 0)
{
dt.Rows.Add();
foreach (string cell in row.Split(','))
{
if (i < 9)
{
temp = cell.Replace("\n", "");
temp = temp.Replace("\r", "");
dt.Rows[dt.Rows.Count - 1][i] = temp;
i++;
}
}
}
else
{
first_row_flag++; //get rid of first row
}
}
}
The number of columns in each row is 9. Thats why I use i to make sure I will not read unexpected data in 10th column.
Here is the stack trace: