Exemplo n.º 1
0
        static void CustomerCSV()
        {
            CustomerData        cud          = new CustomerData();
            Customers           CUST         = new Customers();
            List <Customers>    CustomerList = new List <Customers>();
            NorthwindDBEntities db           = new NorthwindDBEntities();

            ConsoleTable TablaCust = new ConsoleTable("Código", "Nombre");

            foreach (Customers List in cud.Listado <Customers>())
            {
                TablaCust.AddRow(List.CustomerID, List.ContactName);
            }
            TablaCust.Write(Format.Alternative);

            string RutaCSV = Properties.Settings.Default.RutaCSV;

            string[] lines = File.ReadAllLines(RutaCSV);
            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];
                if (line.Contains(","))
                {
                    var    split      = line.Split(',');
                    string id         = split[0];
                    string CompName   = split[1];
                    string ContName   = split[2];
                    string ContTitle  = split[3];
                    string Address    = split[4];
                    string City       = split[5];
                    string Region     = split[6];
                    string PostalCode = split[7];
                    string Country    = split[8];
                    string Phone      = split[9];
                    string Fax        = split[10];

                    CUST              = new Customers();
                    CUST.CustomerID   = id;
                    CUST.CompanyName  = CompName;
                    CUST.ContactName  = ContName;
                    CUST.ContactTitle = ContTitle;
                    CUST.Address      = Address;
                    CUST.City         = City;
                    CUST.Region       = Region;
                    CUST.PostalCode   = PostalCode;
                    CUST.Country      = Country;
                    CUST.Phone        = Phone;
                    CUST.Fax          = Fax;
                    CustomerList.Add(CUST);
                }
            }

            foreach (Customers c in CustomerList)
            {
                if (db.Customers.Any(ct => ct.CustomerID == c.CustomerID))
                {
                    var resultCUST = cud.Listado <Customers>().Find(a => a.CustomerID == c.CustomerID);
                    try
                    {
                        cud.Model.Entry(resultCUST).State = EntityState.Detached;
                        cud.Actualizar(c);
                    }
                    catch (Exception msj)
                    {
                        Console.WriteLine("\n\n  Ha ocurrido un error actualizando datos: " + msj.Message.ToString());
                    }
                }
                else
                {
                    try
                    {
                        cud.Agregar(c);
                    }
                    catch (Exception msj)
                    {
                        Console.WriteLine("\n\n  Ha ocurrido un error insertando datos: " + msj.Message.ToString());
                    }
                }
            }

            Console.Write("\n");
            Console.WriteLine("\n Presione Enter para continuar...");
            Console.ReadLine();
            Console.Clear();
        }