public void Update(string fileName, MathRule mathRule) { System.Xml.Linq.XDocument document = System.Xml.Linq.XDocument.Load(fileName); var node = (from t in document.Descendants("Column") where t.Element("DataBase").Value == mathRule.ColumnInDB select t).FirstOrDefault(); // check exist element excel string[] arr = node.Element("Excel").Value.Split(';'); List <string> list = new List <string>(); for (int i = 0; i < arr.Length; i++) { list.Add(arr[i].Trim()); } if (!list.Contains(mathRule.ColumnInExcel.Trim())) { node.Element("Excel").Value += ";" + mathRule.ColumnInExcel; } // end check //node.Attribute("AllowBlank").Value = mathRule.AllowBlank; //node.Attribute("DataType").Value = mathRule.DataType; //node.Attribute("DisplayOnGrid").Value = mathRule.DisplayOnGrid.ToString(); //if (!string.IsNullOrEmpty(mathRule.DefaultValue)) // node.Attribute("DefaultValue").Value = mathRule.DefaultValue; //else // node.Attribute("DefaultValue").Value = ""; document.Save(fileName); }
public void UpdateTuDien(string fileName, MathRule mathRule) { System.Xml.Linq.XDocument document = System.Xml.Linq.XDocument.Load(fileName); var node = (from t in document.Descendants("Column") where t.Element("DataBase").Value == mathRule.ColumnInDB select t).FirstOrDefault(); node.Element("Excel").Value = mathRule.ColumnInExcel; document.Save(fileName); }
private void InsertData() { #region chuẩn bị //if (ruleList == null) ruleList = new XMLProcess().GetAll(XDocument.Load(Server.MapPath(MathRuleXmlUrl))); string sql = "insert into " + this.TableName + "({0}) values"; string strUpdate = "update " + this.TableName + " set {0} where {1}"; string col = ""; List <MathRule> mr = new List <MathRule>(); //Lấy ra các cột cần insert foreach (var item in RowSelectionModel1.SelectedRows) { col += "[" + item.RecordID + "],"; MathRule mathRule = ruleList.Where(t => t.ColumnInDB == item.RecordID).FirstOrDefault(); if (mathRule != null && mathRule.ColumnInExcel != "") { mr.Add(new MathRule() { ColumnInDB = item.RecordID, ColumnInExcel = mathRule.ColumnInExcel, DataType = mathRule.DataType, AllowBlank = mathRule.AllowBlank, DisplayOnGrid = mathRule.DisplayOnGrid, DefaultValue = mathRule.DefaultValue }); } } //col += "[MA_DONVI],"; if (col.LastIndexOf(",") != -1) { sql = string.Format(sql, col.Remove(col.LastIndexOf(","))); } string ExcelFileName = Server.MapPath(ExcelStoreFolder + "/") + fUpload.FileName; //Lấy các cột trong file Excel IEnumerable <string> ColumnInExcelFile = ExcelEngine.GetInstance().GetColumnName(ExcelFileName, cbSheetName.SelectedItem.Value); int completedRows = 0, updatedRows = 0; int errorLine = 1; List <object> errorData = new List <object>(); #endregion List <Row> dataExcel = null; dataExcel = ExcelEngine.GetInstance().GetDataFromExcel(ExcelFileName, cbSheetName.SelectedItem.Value, 0, MaxRecord); #region lấy dữ liệu và insert foreach (var row in dataExcel) { bool isExist = false; // đã tồn tại mã cán bộ hay chưa if (string.IsNullOrEmpty(row[0])) { break; } // lấy Trường mã cán bộ trong file Excel MathRule mr1 = ruleList.Where(t => t.ColumnInDB == "MA_CB").FirstOrDefault(); DAL.HOSO hs = new HoSoController().GetByMaCB(row[mr1.ColumnInExcel]); if (hs != null) { isExist = true; } string v = "", u = ""; foreach (MathRule rule in mr) { // bỏ qua prkey tự tăng if (rule.ColumnInDB.Equals("PR_KEY")) { continue; } if (rule.DisplayOnGrid == false && string.IsNullOrEmpty(rule.DefaultValue) == false) { if (isExist == true) { if (rd_CapNhatBanGhiTonTai.Checked == true) { u += "[" + rule.ColumnInDB + "] = N'" + GetDefaultValue(rule.DefaultValue) + "',"; } } else { v += "N'" + GetDefaultValue(rule.DefaultValue) + "',"; } continue; } if (rule.ColumnInExcel.Contains(";") == false) { string vTmp = row[rule.ColumnInExcel].ToString(); if (string.IsNullOrEmpty(vTmp)) { vTmp = GetDefaultValue(rule.DefaultValue); } vTmp = GetCastedData(rule.DataType.ToLower(), vTmp); if (vTmp == "$$$") { errorData.Add(new { Data = row[rule.ColumnInExcel].ToString(), ErrorMessage = "Dữ liệu tại cột \"" + rule.ColumnInExcel + "\" dòng " + (errorLine + 1) + " trong tệp tin excel chưa tồn tại trong danh mục.", LineInExcel = errorLine, }); continue; } if (isExist == true) { if (rd_CapNhatBanGhiTonTai.Checked == true) { u += "[" + rule.ColumnInDB + "] = N'" + vTmp + "',"; } } else { v += "N'" + vTmp + "',"; } } else { string[] tmp = rule.ColumnInExcel.Split(';'); foreach (var item in tmp) { if (string.IsNullOrEmpty(item) == false && ColumnInExcelFile.Contains(item)) { if (string.IsNullOrEmpty(row[item].ToString())) { if (isExist == true) { if (rd_CapNhatBanGhiTonTai.Checked == true) { u += "[" + rule.ColumnInDB + "] = N'" + GetDefaultValue(rule.DefaultValue) + "',"; } } else { v += "N'" + GetDefaultValue(rule.DefaultValue) + "',"; } } else { string tmp1 = GetCastedData(rule.DataType.ToLower(), row[item]); if (tmp1 == "$$$") { errorData.Add(new { Data = row[item].ToString(), ErrorMessage = "Dữ liệu tại cột \"" + rule.ColumnInExcel + "\" dòng " + (errorLine + 1) + " trong tệp tin excel chưa tồn tại trong danh mục.", LineInExcel = errorLine, }); continue; } if (isExist == true) { if (rd_CapNhatBanGhiTonTai.Checked == true) { u += "[" + rule.ColumnInDB + "] = N'" + tmp1 + "',"; } } else { v += "N'" + tmp1 + "',"; } } break; } else { //v += "N'',"; } } } } //v += "N'" + Session["MaDonVi"] + "',"; errorLine++; try { // insert string query = string.Empty; if (v != "") { if (v.LastIndexOf(",") != -1) { query = sql + "(" + v.Remove(v.LastIndexOf(",")) + ")"; } DataHandler.GetInstance().ExecuteNonQuery(query); completedRows++; } // update if (u != "") { string queryUpdate = string.Empty; if (u.LastIndexOf(",") != -1) { queryUpdate = string.Format(strUpdate, u.Remove(u.LastIndexOf(",")), " MA_CB = N'" + row[mr1.ColumnInExcel] + "'"); } DataHandler.GetInstance().ExecuteNonQuery(queryUpdate); updatedRows++; } } catch (Exception ex) { string Er = string.Empty; if (ex.Message.Contains("Cannot insert duplicate key in object")) { Er = GlobalResourceManager.GetInstance().GetErrorMessageValue("DuplicateKey"); } else if (ex.Message.Contains("Error converting data type nvarchar to numeric.")) { Er = GlobalResourceManager.GetInstance().GetErrorMessageValue("NvarcharToNumeric"); } else if (ex.Message.Contains("Conversion failed when converting date and/or time from character string.")) { Er = GlobalResourceManager.GetInstance().GetErrorMessageValue("DateFromString"); } else { Er = ex.Message; } errorData.Add(new { Data = v.Replace("N", "").Replace("'", ""), ErrorMessage = Er, LineInExcel = errorLine, }); } } #endregion // delete file FileInfo file = new FileInfo(ExcelFileName); if (file.Exists) { file.Delete(); } #region thông báo kết quả insert lblCompletedRow.Html = "<span style='color:#15428B;font-weight:bold;display:block;margin-bottom:6px;'>Tổng số dòng được thêm thành công : " + completedRows + " dòng</span>"; if (updatedRows > 0) { lblUpdatedRow.Html = "<span style='color:#15428B;font-weight:bold;display:block;margin-bottom:6px;'>Tổng số dòng được cập nhật thành công : " + updatedRows + " dòng</span>"; } else { lblUpdatedRow.Html = ""; } // lblError.Text = "Tổng số dòng bị lỗi : " + errorData.Count(); grp_ErrorRows.Title = "Tổng số dòng bị lỗi : " + errorData.Count() + " dòng"; Store2.DataSource = errorData; Store2.DataBind(); #endregion }