/// <summary> /// 插入或更新 自动替换数据. /// </summary> /// <param name="autoReplaceData"></param> /// <returns></returns> public bool InsertOrUpdateAutoReplace(AutoReplace autoReplaceData) { try { if (String.IsNullOrEmpty(autoReplaceData.SourceText)) { ResultMessage = "原始文本不能为空!"; return(false); } if (String.IsNullOrEmpty(autoReplaceData.MachineText)) { ResultMessage = "机翻文本不能为空!"; return(false); } if (String.IsNullOrEmpty(autoReplaceData.TranslateText)) { ResultMessage = "结果文本不能为空!"; return(false); } using (MyTranslateContext context = new MyTranslateContext()) { AutoReplace dbData = context.AutoReplaces.FirstOrDefault(p => p.SourceText == autoReplaceData.SourceText); if (dbData == null) { // 数据库中不存在. // 新增. autoReplaceData.BeforeInsertOperation(); // 插入. context.AutoReplaces.Add(autoReplaceData); } else { dbData.MachineText = autoReplaceData.MachineText; dbData.TranslateText = autoReplaceData.TranslateText; dbData.Status = autoReplaceData.Status; dbData.BeforeUpdateOperation(); } // 物理保存. context.SaveChanges(); } return(true); } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 导出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExp_Click(object sender, EventArgs e) { if (this.mainDataList == null || this.mainDataList.Count == 0) { MyMessage.Warn("当前没有数据可导出!"); return; } if (this.saveFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) { // 用户取消. return; } List <AutoReplace> todoList = new List <AutoReplace>(); foreach (var item in this.mainDataList) { AutoReplace expData = new AutoReplace(); CommonModelCopyer.ModelCopy(item, expData); expData.Status = item.Status; todoList.Add(expData); } // 输出 UTF-8 的 XML 文件. XmlSerializer xs = new XmlSerializer(typeof(List <AutoReplace>)); using (StreamWriter sw = new StreamWriter(this.saveFileDialog1.FileName)) { xs.Serialize(sw, todoList); } MyMessage.Success("导出完毕!"); }
/// <summary> /// 双击编辑. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvAutoReplace_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { // 取得行号. int index = e.RowIndex; if (index < 0) { // 忽略为 负数的行号. return; } // 取得指定行的数据. AutoReplace currentData = mainDataList[index]; FormEditAutoReplace editFrom = new FormEditAutoReplace(); editFrom.EditAutoReplace = currentData; editFrom.ShowDialog(); }
/// <summary> /// 保存. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.txtSourceText.Text)) { MyMessage.Warn("原始文本 必须输入!"); this.txtSourceText.Focus(); this.DialogResult = System.Windows.Forms.DialogResult.None; return; } if (String.IsNullOrEmpty(this.txtMachineText.Text)) { MyMessage.Warn("机翻文本 必须输入!"); this.txtMachineText.Focus(); this.DialogResult = System.Windows.Forms.DialogResult.None; return; } if (String.IsNullOrEmpty(this.txtTranslateText.Text)) { MyMessage.Warn("翻译文本 必须输入!"); this.txtTranslateText.Focus(); this.DialogResult = System.Windows.Forms.DialogResult.None; return; } if (!MyMessage.Makesure("确认要更新自动替换数据么?")) { this.DialogResult = System.Windows.Forms.DialogResult.None; return; } if (currentAutoReplace == null) { currentAutoReplace = new AutoReplace(); } // 原始文本. currentAutoReplace.SourceText = this.txtSourceText.Text; // 机翻文本 currentAutoReplace.MachineText = this.txtMachineText.Text; // 翻译文本 currentAutoReplace.TranslateText = this.txtTranslateText.Text; // 有效性. currentAutoReplace.IsActive = this.chkActive.Checked; bool result = autoReplaceService.InsertOrUpdateAutoReplace(currentAutoReplace); if (result) { MyMessage.Success("更新成功!"); this.Close(); } else { MyMessage.Fail(autoReplaceService.ResultMessage); this.DialogResult = System.Windows.Forms.DialogResult.None; } }