public HttpResponseBase productcategoryinto() { string json = string.Empty; DTExcel.Clear(); DTExcel.Columns.Clear(); string newName = string.Empty; DTExcel.Columns.Add("產品ID", typeof(String)); DTExcel.Columns.Add("類別ID", typeof(String)); DTExcel.Columns.Add("不能匯入的原因", typeof(String)); int result = 0; int count = 0;//總匯入數 int errorcount = 0; int chongfucount = 0; StringBuilder strsql = new StringBuilder(); try { if (Request.Files["ImportFileMsg"] != null && Request.Files["ImportFileMsg"].ContentLength > 0) { HttpPostedFileBase excelFile = Request.Files["ImportFileMsg"]; //FileManagement fileManagement = new FileManagement(); newName = Server.MapPath(excelPath) + excelFile.FileName; excelFile.SaveAs(newName); DataTable dt = new DataTable(); dt = CsvHelper.ReadCsvToDataTable(newName, true); _productMgr = new ProductMgr(connectionString); if (dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { try { int a = Convert.ToInt32(dr[0]);//產品ID int b = Convert.ToInt32(dr[1]);//類別ID if (_productMgr.Yesornoexist(a, b) > 0)//大於1表示該數據庫內存在該值 { DataRow drtwo = DTExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "數據庫中該行數據已存在"; DTExcel.Rows.Add(drtwo); chongfucount++; continue; //someproduct_id = someproduct_id + a + ','; //someproduct_categroy_id = someproduct_categroy_id + b + ','; } else//當數據不存在時進行添加數據 { int product_id = _productMgr.Yesornoexistproduct(a); int category_id = _productMgr.Yesornoexistproductcategory(b); if (product_id > 0 && category_id > 0) { count++; strsql.AppendFormat(@"insert into product_category_set(product_id,category_id)values('{0}','{1}');", a, b); continue; } else if (product_id == 0 && category_id == 0) { DataRow drtwo = DTExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "類別ID和產品ID在類別表和產品表中均不存在"; DTExcel.Rows.Add(drtwo); errorcount++; continue; } else if (product_id > 0 && category_id == 0) { DataRow drtwo = DTExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "類別ID在類別表中不存在"; DTExcel.Rows.Add(drtwo); errorcount++; continue; } else if (product_id == 0 && category_id > 0) { DataRow drtwo = DTExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "產品ID在產品表中不存在"; DTExcel.Rows.Add(drtwo); errorcount++; continue; } } } catch { DataRow drtwo = DTExcel.NewRow(); drtwo[0] = dr[0].ToString(); drtwo[1] = dr[1].ToString(); drtwo[2] = "數據異常"; DTExcel.Rows.Add(drtwo); errorcount++; continue; } } if (strsql.ToString().Trim() != "") { result = _productMgr.Updateproductcategoryset(strsql.ToString()); if (result > 0) { json = "{success:true,total:" + count + ",error:" + errorcount + ",repeat:" + chongfucount + "}"; } else { json = "{success:false}"; } } else { json = "{success:true,total:" + 0 + ",error:" + errorcount + ",repeat:" + chongfucount + "}"; } } else { json = "{success:true,total:" + 0 + ",error:" + 0 + ",repeat:" + 0 + "}"; } } } catch (Exception ex) { newName = string.Empty; DTExcel.Clear(); DTExcel.Columns.Clear(); Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); json = "{success:false,data:" + "" + "}"; } this.Response.Clear(); this.Response.Write(json); this.Response.End(); return this.Response; }