Esempio n. 1
0
        static void Main(string[] args)
        {
            var filename = @"clients.csv";

            if (args.Length > 2 && !string.IsNullOrEmpty(args[1]))
            {
                filename = args[1];
            }


            DateTime start   = DateTime.Now;
            int      count   = 0;
            var      csvconf = new CsvHelper.Configuration.CsvConfiguration()
            {
                HasHeaderRecord = true,
                IsStrictMode    = true,
                IsCaseSensitive = false,
                Quote           = '"'
            };


            using (CsvHelper.CsvReader reader = new CsvHelper.CsvReader(new System.IO.StreamReader(filename), csvconf))
            {
                var provider     = System.Globalization.CultureInfo.GetCultureInfo("en-gb");            //.InvariantCulture;
                var rowConverter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(zz));
                System.Threading.Thread.CurrentThread.CurrentCulture = provider;
                User admin = null;
                using (var db = new ccEntities())
                {
                    admin = db.Users.Single(f => f.UserName == "admin");
                }

                while (reader.Read())
                {
                    zz r = null;
                    count++;
                    try { r = reader.GetRecord <zz>(); }
                    catch (IndexOutOfRangeException) { r = reader.GetRecord <zz>(); }
                    {
                        Client client = null;
                        try
                        {
                            client = (Client)rowConverter.ConvertTo(r, typeof(Client));
                        }
                        catch (InvalidOperationException ex)
                        {
                            Console.WriteLine("exception at row " + reader.Parser.Row.ToString() + ": " + ex.Message);
                            Console.ReadKey();
                        }
                        var updateDate = DateTime.Now;
                        client.UpdatedAt   = updateDate;
                        client.CreatedAt   = updateDate;
                        client.UpdatedById = admin.Id;

                        using (var db = new ccEntities())
                        {
                            var existing = db.Clients.SingleOrDefault(f => f.Id == client.Id);
                            if (existing == null)
                            {
                                if (client.Id == default(int))
                                {
                                    db.Clients.AddObject(client);
                                }
                                else
                                {
                                    var inserted = db.InsertClient(client.Id, client.FirstName, client.LastName, client.JoinDate, client.ApprovalStatusId, client.UpdatedById, client.UpdatedAt, client.CreatedAt).FirstOrDefault();
                                    var entry    = db.ObjectStateManager.GetObjectStateEntry(inserted);
                                    entry.ApplyCurrentValues(client);
                                    db.SaveChanges();
                                }
                            }
                            else
                            {
                                var entry = db.ObjectStateManager.GetObjectStateEntry(existing);
                                entry.ApplyCurrentValues(client);
                            }
                            var rowsUpdated = db.SaveChanges();
                            Console.WriteLine("Row: " + reader.Parser.Row + ", clientid:" + client.Id);
                        }
                        if (reader.Parser.Row % 100 == 0)
                        {
                            Console.WriteLine("elapsed: " + (DateTime.Now - start).TotalSeconds);
                        }
                    }
                }
            }

            Console.WriteLine("count: " + count + "elapsed: " + (DateTime.Now - start).TotalSeconds);

            Console.ReadKey();
        }