简体   繁体   中英

C# DbDataReader populating List result in OutOfMemoryException

I am currently trying to read a large table from an compact ce database containing aprox 3-4 million rows. The database size i currently 832MB. Populating a list with the records is throwing OutOfMemoryException

The mockup code:

    using (var con = new DomainContext())
    {
        foreach (var item in con.logRecords)
        {
            if (item.Info != null && item.Info != "")
                item.Timestamp = DateTime.ParseExact(item.Info, "MM.dd.yyyy HH:mm:ss.fff", culture).Ticks;
        }
        con.SaveChanges();

    }

New approach, still not getting it to work....

        Task.Factory.StartNew(() =>
        {
            using (var con = new DomainContext())
            {
                for (int i = 0; i < 300; i++)
                {
                    try
                    {
                        var temp = con.logRecords.Where(p => p.Id <= i * 10000 + 10000 && p.Id >= i * 10000);

                        foreach (var item in temp)
                        {

                            if (item.Info != null && item.Info != "")
                                item.Timestamp = DateTime.ParseExact(item.Info, "MM.dd.yyyy HH:mm:ss.fff", culture).Ticks;
                        }

                        con.SaveChanges();

                    }
                    catch { }
                    GC.Collect();
                    Console.WriteLine(i.ToString());


                }
            }
        });

I used native SQL, parsed timestamp to SQL timestamp then found number off seconds since 1970 using DATEDIFF ( datepart , startdate , enddate ). Added number of seconds since year 0. I lose millisecond part, but i guess this is the next best thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM