I am trying to load a large flat file (250 Mb) in a datatable but after loading a considerable amount of records (4 M), I throw the error 'System.OutOfMemoryException'
DataTable listaTX = new DataTable("listaTX");
listaTX.Columns.Add("CodDep", typeof(int));
listaTX.Columns.Add("CodMun", typeof(int));
listaTX.Columns.Add("CodZon", typeof(int));
listaTX.Columns.Add("CodPue", typeof(String));
listaTX.Columns.Add("Mesa", typeof(int));
listaTX.Columns.Add("CodJal", typeof(int));
listaTX.Columns.Add("Comunicado", typeof(int));
listaTX.Columns.Add("CodCirc", typeof(int));
listaTX.Columns.Add("CodPar", typeof(int));
listaTX.Columns.Add("CodCan", typeof(int));
listaTX.Columns.Add("Votos", typeof(int));
listaTX.Columns.Add("CodTX", typeof(int));
using (FileStream fs = File.Open(ruta, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
try
{
while ((linea = sr.ReadLine()) != null)
{
DataRow fila = listaTX.NewRow();
fila[0] = linea.Substring(0, 2);
fila[1] = linea.Substring(2, 3);
fila[2] = linea.Substring(5, 2);
fila[3] = linea.Substring(7, 2);
fila[4] = linea.Substring(9, 6);
fila[5] = linea.Substring(15, 2);
fila[6] = linea.Substring(17, 4);
fila[7] = linea.Substring(21, 1);
fila[8] = linea.Substring(22, 3);
fila[9] = linea.Substring(25, 3);
fila[10] = linea.Substring(28, 8);
fila[11] = linea.Substring(36, 7);
listaTX.Rows.Add(fila);
}
}
}
If in the property box of the project I disable the option "Prefer 32 - bit" it works for me but I did the test on a 32 bit machine and the mentioned error comes out.
Is there a way to optimize the code or another method that allows me to load this large file into a datatable?