using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data; using ICSharpCode.Core; using Mesnac.Action.Base; using Mesnac.Codd.Session; namespace Mesnac.Action.ChemicalWeighing.Alarm.PmtAlarm { /// /// 导入报警参数业务 /// public class ImportAction: ChemicalWeighingAction, IAction { private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption")); //提示 public void Run(RuntimeParameter runtime) { string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg")); //导入操作会覆盖当前所有报警信息!是否继续? if (MessageBox.Show(msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } base.RunIni(runtime); //必须要调用的 ICSharpCode.Core.LoggingService.Debug("报警参数-导入..."); DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { ICSharpCode.Core.LoggingService.Error("获取本地数据连接失败..."); return; } OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "xls files(*.xls)|*.xls"; ofd.AddExtension = true; DialogResult result = ofd.ShowDialog(); if (result == DialogResult.OK) { try { string fileName = ofd.FileName; if (!String.IsNullOrEmpty(fileName)) { DataSet ds = Mesnac.Basic.DataToFileHandler.Instance.FromExcel(fileName); //判断导入Excel表格式的合法性 if (!ds.Tables[0].Columns.Contains("Alarm_PLC") || !ds.Tables[0].Columns.Contains("Alarm_Block") || !ds.Tables[0].Columns.Contains("Alarm_Alias") || !ds.Tables[0].Columns.Contains("Alarm_Cn_Info") ) { string msg4 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg4")); //导入的Excel表格式不正确!请检查后重试! MessageBox.Show(msg4); return; } string del = "truncate table Pmt_Alarm"; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = del; dbHelper.ExecuteNonQuery(); //truncate table PmtAlarm if (ds != null && ds.Tables.Count > 0) { string sqlstr = @"insert into Pmt_Alarm(Equip_Code, Alarm_PLC, Alarm_Block, Alarm_Alias, Alarm_Cn_Info) values (@EquipCode, @AlarmPLC, @AlarmBlock, @Alarm_Alias, @Alarm_Cn_Info)"; foreach (DataRow row in ds.Tables[0].Rows) { dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@EquipCode", row["Equip_Code"]); dbHelper.AddParameter("@AlarmPLC", row["Alarm_PLC"]); dbHelper.AddParameter("@AlarmBlock", row["Alarm_Block"]); dbHelper.AddParameter("@Alarm_Alias", row["Alarm_Alias"]); dbHelper.AddParameter("@AlarmName", row["Alarm_Cn_Info"]); dbHelper.ExecuteNonQuery(); } string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg1")); //从Excel导入报警参数数据完毕! ICSharpCode.Core.LoggingService.Info(msg1); #region 记录操作日志 base.DBLog(msg1); #endregion MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg2")); //从Excel中导入报警参数数据失败:Excel中没有数据! ICSharpCode.Core.LoggingService.Warn(msg2); #region 记录操作日志 base.DBLog(msg2); #endregion MessageBox.Show(String.Format(msg2), Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } catch (Exception ex) { string msg3 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg3")); //从Excel导入报警参数数据失败:{0}! msg3 = String.Format(msg3, ex.Message); ICSharpCode.Core.LoggingService.Error(msg3); #region 记录操作日志 base.DBLog(msg3); #endregion MessageBox.Show(msg3, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning); } //刷新DataGridView数据 DbMCControl dgPmtAlarmInfo = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_Alarm").FirstOrDefault(); if (dgPmtAlarmInfo == null || !(dgPmtAlarmInfo.BaseControl is DataGridView)) { ICSharpCode.Core.LoggingService.Warn("报警参数-导入-缺少报警参数DataGridView控件..."); runtime.IsReturn = false; return; } DataTable table = dbHelper.GetDataTableBySql(dgPmtAlarmInfo.BaseControl.ActionDataSource); dgPmtAlarmInfo.BaseControl.BindDataSource = table; } } } }