jsandv
jsandv

Reputation: 306

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());


                }
            }
        });

Upvotes: 0

Views: 70

Answers (1)

jsandv
jsandv

Reputation: 306

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.

Upvotes: 0

Related Questions