Пример #1
0
        public bool LoadCustomer()
        {
            bool ret = true;

            List <string> csv_title  = new List <string>();
            bool          first_line = true;

            try
            {
                customer     = new List <AccessCustomers>();
                lStatus.Text = "";
                int row_index = 0;
                using (CsvFileReader reader = new CsvFileReader(this.lCustomerFilename.Text))
                {
                    CsvRow row = new CsvRow();
                    while (reader.ReadRow(row))
                    {
                        row_index++;
                        lStatus.Text = "Read row " + row_index.ToString();
                        Application.DoEvents();
                        Application.DoEvents();
                        if (first_line) // need to parse the title
                        {
                            // check if the title missing
                            if (row.ValidRow())
                            {
                                csv_title  = row.ToList();
                                first_line = false;
                            }
                            else
                            {
                                ret = false;
                                break;
                            }
                        }
                        else
                        {
                            // parse data
                            if (row.ValidRow())
                            {
                                AccessCustomers model = new AccessCustomers();
                                try
                                {
                                    // determine which field we need to put into object
                                    for (int i = 0; i < csv_title.Count; i++)
                                    {
                                        switch (csv_title[i])
                                        {
                                        case "contactid":
                                            try
                                            {
                                                model.contactid = long.Parse(row[i]);
                                            }
                                            catch
                                            {
                                                model.contactid = 0;
                                            }
                                            break;

                                        case "firstname":
                                            try
                                            {
                                                model.firstname = row[i];
                                            }
                                            catch
                                            {
                                                model.firstname = "";
                                            }
                                            break;

                                        case "lastname":
                                            try
                                            {
                                                model.lastname = row[i];
                                            }
                                            catch
                                            {
                                                model.lastname = "";
                                            }
                                            break;

                                        case "dob":
                                            try
                                            {
                                                model.dob = row[i];
                                            }
                                            catch
                                            {
                                                model.dob = "";
                                            }
                                            break;

                                        case "address":
                                            try
                                            {
                                                model.address = row[i];
                                            }
                                            catch
                                            {
                                                model.address = "";
                                            }
                                            break;

                                        case "city":
                                            try
                                            {
                                                model.city = row[i];
                                            }
                                            catch
                                            {
                                                model.city = "";
                                            }
                                            break;

                                        case "state":
                                            try
                                            {
                                                model.state = row[i];
                                            }
                                            catch
                                            {
                                                model.state = "";
                                            }
                                            break;

                                        case "postcode":
                                            try
                                            {
                                                model.postcode = row[i];
                                            }
                                            catch
                                            {
                                                model.postcode = "";
                                            }
                                            break;

                                        case "country":
                                            try
                                            {
                                                model.country = row[i];
                                            }
                                            catch
                                            {
                                                model.country = "";
                                            }
                                            break;

                                        case "phone":
                                            try
                                            {
                                                model.phone = row[i];
                                            }
                                            catch
                                            {
                                                model.phone = "";
                                            }
                                            break;

                                        case "email":
                                            try
                                            {
                                                model.email = row[i];
                                            }
                                            catch
                                            {
                                                model.email = "";
                                            }
                                            break;

                                        case "website":
                                            try
                                            {
                                                model.website = row[i];
                                            }
                                            catch
                                            {
                                                model.website = "";
                                            }
                                            break;

                                        case "password":
                                            try
                                            {
                                                model.password = row[i];
                                            }
                                            catch
                                            {
                                                model.password = "";
                                            }
                                            break;

                                        default:
                                            break;
                                        }
                                    }

                                    customer.Add(model);
                                }
                                catch (Exception ex)
                                {
                                    // do not thing, forget this coupon
                                    UpdateInfo("CUSTOMER PARSER . Id = " + model.contactid + " row = " + row_index.ToString() + "; Error: " + ex.Message);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return(ret);
        }
Пример #2
0
        //      public bool ConnectToAccess()
        //      {
        //          FileAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
        //"Data Source=" + Application.StartupPath + "\\db.mdb;Persist Security Info=False");
        //          try
        //          {
        //              FileAccess.Open();
        //          }
        //          catch
        //          {
        //              UpdateInfo("Can not open Access db");
        //              return false;
        //          }
        //          return true;
        //      }

        public bool LoadData()
        {
            bool ret = true;

            List <string> csv_title  = new List <string>();
            bool          first_line = true;

            try
            {
                data         = new List <CouponModel>();
                lStatus.Text = "";
                int row_index = 0;
                using (CsvFileReader reader = new CsvFileReader(this.lSCVFile.Text))
                {
                    CsvRow row = new CsvRow();
                    while (reader.ReadRow(row))
                    {
                        row_index++;
                        lStatus.Text = "Read row " + row_index.ToString();
                        Application.DoEvents();
                        //Application.DoEvents();
                        if (first_line) // need to parse the title
                        {
                            // check if the title missing
                            if (row.ValidRow())
                            {
                                csv_title  = row.ToList();
                                first_line = false;
                            }
                            else
                            {
                                ret = false;
                                break;
                            }
                        }
                        else
                        {
                            // parse data
                            if (row.ValidRow())
                            {
                                CouponModel model = new CouponModel();
                                try
                                {
                                    // determine which field we need to put into object
                                    for (int i = 0; i < csv_title.Count; i++)
                                    {
                                        switch (csv_title[i])
                                        {
                                        case "couponname":
                                            model.couponname = row[i].Trim();
                                            break;

                                        case "couponId":
                                            model.couponId = long.Parse(row[i]);
                                            break;

                                        case "couponsecuritycode":
                                            model.couponsecuritycode = row[i].Trim();
                                            break;

                                        case "couponamount":
                                            model.couponamount = double.Parse(row[i]);
                                            break;

                                        case "couponcurrency":
                                            model.couponcurrency = row[i].Trim().ToUpper();
                                            break;

                                        case "couponstartdate":
                                            model.couponstartdate = DateTime.Parse(row[i]);
                                            break;

                                        case "couponenddate":
                                            model.couponenddate = DateTime.Parse(row[i]);
                                            break;

                                        case "couponlimit":
                                            model.couponlimit = int.Parse(row[i]);
                                            break;

                                        case "couponusedcount":
                                            model.couponusedcount = int.Parse(row[i]);
                                            break;

                                        case "couponproducts":
                                            model.couponproducts = row[i];
                                            break;

                                        case "couponlastuseddate":

                                            try
                                            {
                                                model.couponlastuseddate = row[i];
                                            }
                                            catch
                                            {
                                                model.couponlastuseddate = "";
                                            }
                                            break;

                                        case "couponcomment":

                                            try
                                            {
                                                model.couponcomment = row[i];
                                            }
                                            catch
                                            {
                                                model.couponcomment = "";
                                            }
                                            break;

                                        case "couponother":
                                            try
                                            {
                                                model.couponother = row[i];
                                            }
                                            catch
                                            {
                                                model.couponother = "";
                                            }
                                            break;

                                        default:
                                            break;
                                        }
                                    }

                                    data.Add(model);
                                }
                                catch (Exception ex)
                                {
                                    // do not thing, forget this coupon
                                    UpdateInfo("COUPON IMPORT . Id = " + model.couponId + " row = " + row_index.ToString() + "; Error: " + ex.Message);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                ret = false;
            }
            return(ret);
        }