Exemplo n.º 1
0
        private void cmdimport_Click(object sender, EventArgs e)
        {
            txtmsg.Visible = true;
            string line;
            IList<Product> products = new List<Product>();

            try
            {
                if (!txtpath.Text.Equals(""))
                {

                    if (File.Exists(path))
                    {

                        txtmsg.Text = "���ѧ�Ӣ�������� .........";
                        StreamReader file = null;
                        try
                        {
                            file = new StreamReader(path, Encoding.GetEncoding("windows-874"));
                            int i = 0;
                            Product product = null;
                            ProductService service = new ProductService();
                            int j = 1;
                            int count = 0;
                            int x = 0;
                            while ((line = file.ReadLine()) != null)
                            {
                                if (i != 0)
                                {
                                    string[] words = line.Split('\t');
                                    product = new Product();
                                    product.Code = words[0];
                                    product.BarCode = words[1];
                                    product.Name = words[2];
                                    product.Unit = words[3];
                                    product.Price1 = words[4];
                                    product.Price3 = words[5];
                                    product.Price4 = words[6];
                                    if (words[7] != null && !words[7].Equals(""))
                                    {
                                        product.CreateDate = Convert.ToDateTime(words[7]);
                                    }

                                    this.pBar1.Visible = true;
                                    this.pBar1.Value = x;
                                    x += 10;
                                    if (x > 100) x = 0;

                                    Product _product = service.getByBarCode(product.BarCode);

                                    if (_product == null)
                                    {

                                        bool save = service.saveProduct(product);
                                        if (save)
                                        {
                                            Console.WriteLine("Save Compate :" + product.ID);
                                            txtmsg.Visible = true;
                                            txtmsg.Text = "�Ӣ�������� ���º���� �ӹǹ : " + j + " ��¡��";
                                            j++;
                                        }
                                        else
                                        {
                                            Console.WriteLine("Save Not  Compate :" + product.ID);
                                        }

                                    }
                                    else if (checkdate(_product.CreateDate, product.CreateDate) == false)
                                    {

                                        // update
                                        bool update = service.updateProduct(product);
                                        if (update)
                                        {
                                            Console.WriteLine("update Compate :" + product.ID);
                                            txtmsg.Visible = true;
                                            txtmsg.Text = "�Ӣ�������� ���º���� �ӹǹ : " + j + " ��¡��";
                                            j++;
                                        }
                                        else
                                        {
                                            Console.WriteLine("update Not  Compate :" + product.ID);
                                        }

                                    }
                                    count++;

                                    //products.Add(product);
                                }
                                Console.WriteLine(line);
                                Console.WriteLine(i);
                                i++;

                            }

                            // insert product
                            //ProductService service = new ProductService();

                            //int j = 1;
                            //int count = 0;
                            //int x = 0;
                            //foreach (Product p in products)
                            //{
                            //    this.pBar1.Visible = true;
                            //    this.pBar1.Value = x;
                            //    x += 10;
                            //    if (x > 100) x = 0;

                            //    Product _product = service.getByBarCode(p.BarCode);

                            //    if (_product == null )
                            //    {
                            //        //DateTime cdate = _product.CreateDate;

                            //        bool save = service.saveProduct(p);
                            //        if (save)
                            //        {
                            //            Console.WriteLine("Save Compate :" + p.ID);
                            //            txtmsg.Visible = true;
                            //            txtmsg.Text = "�Ӣ�������� ���º���� �ӹǹ : " + j + " ��¡��";
                            //            j++;
                            //        }
                            //        else
                            //        {
                            //            Console.WriteLine("Save Not  Compate :" + p.ID);
                            //        }

                            //    }
                            //    else if (checkdate(_product.CreateDate, p.CreateDate) == false) {

                            //        // update
                            //        bool update = service.updateProduct(p);
                            //        if (update)
                            //        {
                            //            Console.WriteLine("update Compate :" + p.ID);
                            //            txtmsg.Visible = true;
                            //            txtmsg.Text = "�Ӣ�������� ���º���� �ӹǹ : " + j + " ��¡��";
                            //            j++;
                            //        }
                            //        else
                            //        {
                            //            Console.WriteLine("update Not  Compate :" + p.ID);
                            //        }

                            //    }
                            //    count++;

                            //}

                            if (count == products.Count)
                            {
                                this.pBar1.Value = 0;
                                this.pBar1.Visible = false;
                            }
                            else if (count == 0)
                            {
                                this.pBar1.Value = 0;
                                this.pBar1.Visible = false;
                            }

                            txtmsg.Text = "�Ӣ�������� ������  : " + (j - 1) + " ��¡��";

                        }
                        finally
                        {
                            if (file != null)
                                file.Close();
                        }
                    }
                    else {

                        txtmsg.Text = "�к���辺�����س���͡!!!";
                    }

                }
                else {

                    MessageBox.Show("��س����͡�������Ѻ�Ӣ�������� !!! ", "����͹");

                }

            }
            catch (Exception ex)
            {
                txtmsg.Visible = true;
                txtmsg.Text = "�������ö�Ӣ���������� ���ͧ�ҡ " + ex.Message;

            }
        }