protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         db.Dispose();
     }
     base.Dispose(disposing);
 }
Exemple #2
0
 public ShoppingCartEntities InsertProductContext(ShoppingCartEntities context, Product product, int count, int commitCount, bool recreateContext)
 {
     context.Set <Product>().Add(product);
     if (count % commitCount == 0)
     {
         context.SaveChanges();
         if (recreateContext)
         {
             context.Dispose();
             context = new ShoppingCartEntities();
             context.Configuration.AutoDetectChangesEnabled = false;
         }
     }
     return(context);
 }
 public void Dispose()
 {
     db.Dispose();
 }
Exemple #4
0
        public ActionResult ImportLargeProduct(HttpPostedFileBase ImportLargeFile)
        {
            //check file format
            FileUploadValidate fs = new FileUploadValidate();

            fs.filesize = 2000;
            ExcelValidate ev      = new ExcelValidate();
            string        message = "";

            if (fs.UploadUserFile(ImportLargeFile))     //判斷檔案是否合法
            {
                if (ev.CheckExcelData(ImportLargeFile)) //判斷excel是否有內容
                {
                    var                  currentWorkSheet = ev.workbook.Worksheets.First();
                    int                  col        = 1;
                    int                  row        = 2;
                    int                  count      = 0;
                    DateTime             time_start = DateTime.Now;
                    ShoppingCartEntities dbn        = null;
                    try
                    {
                        dbn = new ShoppingCartEntities();
                        dbn.Configuration.AutoDetectChangesEnabled = false;

                        foreach (var item in currentWorkSheet.Cells)
                        {
                            ++count;
                            Product product = new Product();
                            if (currentWorkSheet.Cells[row, col].Value != null)
                            {
                                product.ProductID      = currentWorkSheet.Cells[row, col++].Value.ToString();
                                product.ProductName    = currentWorkSheet.Cells[row, col++].Value.ToString();
                                product.ProductExplain = currentWorkSheet.Cells[row, col++].Value.ToString();
                                product.ProductPrice   = Convert.ToDecimal((double)currentWorkSheet.Cells[row, col++].Value);
                                product.Create_Date    = DateTime.Now;
                                product.Delete_Flag    = false;
                                product.Shelf_Flag     = true;
                                byte[] temp = BitConverter.GetBytes(0);
                                product.ProductImg_DB = temp;

                                dbn = InsertProductContext(dbn, product, count, 100, true);
                                col = 1;
                                row++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        dbn.SaveChanges();
                    }
                    finally
                    {
                        if (dbn != null)
                        {
                            dbn.Dispose();
                        }
                    }
                    DateTime time_end = DateTime.Now;
                    TempData["ExcelLargeInsertTime"] = "新增了" + (count - 1) + "筆資料共花了" + (((TimeSpan)(time_end - time_start)).TotalMilliseconds / 1000).ToString() + "秒";
                    logger.Info("Excel Insert Product:" + "新增了" + (count - 1) + "筆資料共花了" + (((TimeSpan)(time_end - time_start)).TotalMilliseconds / 1000).ToString() + "秒");
                    TempData["ExcelLargeResultMessage"] = message;
                }
                else
                {
                    TempData["ExcelLargeResultMessage"] = ev.ErrorMessage;
                }
            }
            else
            {//檔案驗證失敗
                TempData["ExcelLargeResultErrorMessage"] = fs.ErrorMessage;
            }

            return(View("CreateProduct"));
        }