using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using ICSharpCode.Core; using Mesnac.Action.Base; using Mesnac.Controls.Base; using System.Windows.Forms; using Mesnac.Codd.Session; namespace Mesnac.Action.Default.SynchroData { public class Update : DefaultAction, IAction { private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption")); //提示 public void Run(RuntimeParameter runtime) { string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_SynchroData_Update_msg1")); //您确认要修改当前信息吗? if (MessageBox.Show(msg1, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } base.RunIni(runtime); //必须调用 #region 限定只对相同DataSource的数据表进行操作 string mcDataSourceID = String.Empty; if (runtime.Sender is Mesnac.Controls.Base.IBaseControl) { mcDataSourceID = (runtime.Sender as Mesnac.Controls.Base.IBaseControl).MCDataSourceID; } #endregion #region 控件数据合法性验证 bool isValidFlag = true; foreach (DbMCControl control in GetAllDbMCControls()) { if (String.IsNullOrEmpty(mcDataSourceID)) { if (control.BaseControl.IsValid == false) { isValidFlag = false; break; } } else { if (control.BaseControl.MCDataSourceID == mcDataSourceID && control.BaseControl.IsValid == false) { isValidFlag = false; break; } } } if (isValidFlag == false) { string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_SynchroData_Insert_msg2")); //操作失败,操作界面存在验证失败的控件! MessageBox.Show(msg2, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } #endregion ShowMsg(String.Empty); //清除消息显示控件的内容 foreach (DbMCSource dbsource in GetAllDbMCSources()) { if (string.IsNullOrWhiteSpace(dbsource.DesignSource) || string.IsNullOrWhiteSpace(dbsource.DataTable.ToString())) { continue; } if (!String.IsNullOrEmpty(mcDataSourceID) && dbsource.MCDataSource.Site.Name != mcDataSourceID) { continue; } string source = dbsource.DesignSource; string table = dbsource.DataTable; DbHelper dbHelper = NewDbHelper(source); if (dbHelper == null) { continue; } List lstDbMCControl = GetAllDbMCControls(); DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig(); List controls = GetDbMCControlByKey(config.ToString(new string[] { source, table, "GUID" })); foreach (DbMCControl control in controls) { if ((control.BaseControl.DbOptionType != DbOptionTypes.Modify && control.BaseControl.DbOptionType != DbOptionTypes.QueryAndModify) || control.BaseControl.MCValue == null || string.IsNullOrWhiteSpace(control.BaseControl.MCValue.ToString())) { continue; } string objid = control.BaseControl.MCValue.ToString(); dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; StringBuilder sqlsb = new StringBuilder(); if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { sqlsb.Append("UPDATE ").Append(table).Append(" SET "); } else { sqlsb.Append("UPDATE [").Append(table).Append("] SET "); } int iPara = 0; foreach (DbMCControl update_control in lstDbMCControl) { if (update_control.DbMCSource == null || update_control.DbMCSource.DbMCKey != dbsource.DbMCKey || string.IsNullOrWhiteSpace(update_control.DataField) || update_control.DataField.Equals("GUID", StringComparison.CurrentCultureIgnoreCase)) { continue; } if ((base.DbOptionTypesIsModify(control.BaseControl))) { iPara++; string parkey = "@" + update_control.DataField + "_Para" + iPara.ToString(); if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { parkey = ":" + update_control.DataField + "_Para" + iPara.ToString(); } sqlsb.Append(update_control.DataField).Append("=").Append(parkey).Append(","); dbHelper.AddParameter(parkey, update_control.BaseControl.MCValue); } } sqlsb.Remove(sqlsb.Length - 1, 1); sqlsb.Append(" WHERE GUID=").Append("'").Append(objid).Append("'"); if (iPara == 0) { continue; } dbHelper.CommandText = sqlsb.ToString(); dbHelper.ExecuteNonQuery(); } new LastActionRow(runtime, dbsource); } string msg3 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_SynchroData_Update_msg3")); //信息修改成功! ShowMsg(msg3); #region 记录操作日志 base.DBLog(msg3); #endregion } } public class LastActionRow : DatabaseAction { public LastActionRow(RuntimeParameter runtime, DbMCSource dbsource) { base.RunIni(runtime); List lstDbMCControl = GetAllDbMCControls(); foreach (DbMCControl dbgrid in lstDbMCControl) { if (dbgrid.DbMCSource == null || dbgrid.DbMCSource.DbMCKey != dbsource.DbMCKey || (dbgrid.BaseControl as DataGridView) == null) { continue; } DataGridView grid = dbgrid.BaseControl as DataGridView; if (grid.SelectedRows.Count > 0) { this.Index = grid.SelectedRows[0].Index; } else { this.Index = grid.SelectedRows[0].Index; } } } public LastActionRow(RuntimeParameter runtime) { base.RunIni(runtime); } public int Index { get { int iResult = 0; IBaseControl control = this.GetMCControlByKey("SelectRowIndexControl").FirstOrDefault(); if (control != null) { int.TryParse(control.MCValue.ToString(), out iResult); } return iResult; } private set { IBaseControl control = this.GetMCControlByKey("SelectRowIndexControl").FirstOrDefault(); if (control != null) { control.MCValue = value.ToString(); } } } } }