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 Insert : 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_Insert_msg1")); //您确认要添加当前信息吗? if (MessageBox.Show(msg1, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { runtime.IsReturn = true; return; } base.RunIni(runtime); //必须调用 #region 控件数据合法性验证 bool isValidFlag = true; foreach (DbMCControl control in GetAllDbMCControls()) { if (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); runtime.IsReturn = true; return; } #endregion ShowMsg(String.Empty); //清除消息显示控件的信息 foreach (DbMCSource dbsource in GetAllDbMCSources()) { if (string.IsNullOrWhiteSpace(dbsource.DesignSource) || string.IsNullOrWhiteSpace(dbsource.DataTable.ToString())) { continue; } string source = dbsource.DesignSource; string table = dbsource.DataTable; DbHelper dbHelper = NewDbHelper(source); if (dbHelper==null) { continue; } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; List lstDbMCControl = GetAllDbMCControls(); StringBuilder sqlsb_A = new StringBuilder(); StringBuilder sqlsb_B = new StringBuilder(); if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { sqlsb_A.Append("INSERT INTO ").Append(table).Append(" ("); } else { sqlsb_A.Append("INSERT INTO [").Append(table).Append("] ("); } sqlsb_B.Append(") values ("); int iPara = 0; foreach (DbMCControl control in lstDbMCControl) { if (control.DbMCSource == null || control.DbMCSource.DbMCKey != dbsource.DbMCKey || string.IsNullOrWhiteSpace(control.DataField)) { continue; } if (control.DataField.Equals("GUID", StringComparison.CurrentCultureIgnoreCase)) { iPara++; string parkey = "@GUID_Para" + iPara.ToString(); if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { parkey = ":GUID_Para" + iPara.ToString(); } sqlsb_A.Append(control.DataField).Append(","); sqlsb_B.Append(parkey).Append(","); dbHelper.AddParameter(parkey, Guid.NewGuid().ToString().ToUpper()); } else if ((base.DbOptionTypesIsModify(control.BaseControl))) { iPara++; string parkey = "@" + control.DataField + "_Para" + iPara.ToString(); if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { parkey = ":" + control.DataField + "_Para" + iPara.ToString(); } sqlsb_A.Append(control.DataField).Append(","); sqlsb_B.Append(parkey).Append(","); dbHelper.AddParameter(parkey, control.BaseControl.MCValue); } } sqlsb_A.Remove(sqlsb_A.Length - 1, 1); sqlsb_B.Remove(sqlsb_B.Length - 1, 1).Append(")"); if (iPara == 0) { continue; } dbHelper.CommandText = sqlsb_A.ToString() + sqlsb_B.ToString(); dbHelper.ExecuteNonQuery(); } string msg3 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_SynchroData_Insert_msg3")); //信息添加成功! ShowMsg(msg3); #region 记录操作日志 base.DBLog(msg3); #endregion } } }