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 Select : DatabaseAction, IAction { private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption")); //提示 public void Run(RuntimeParameter runtime) { 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 ShowMsg(String.Empty); //清除消息显示控件的内容 int iSubCount = 0; if (!string.IsNullOrWhiteSpace(runtime.Parameters)) { int.TryParse(runtime.Parameters, out iSubCount); } #region 先处理非DataGridView控件 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(); foreach (DbMCControl control in lstDbMCControl) { if (control.DbMCSource == null || control.DbMCSource.DbMCKey != dbsource.DbMCKey || string.IsNullOrWhiteSpace(control.BaseControl.ActionDataSource)) { continue; } if (control.BaseControl is DataGridView) { //跳过DataGridView控件的处理 continue; } string ss = control.BaseControl.ActionDataSource; if (!ss.ToLower().Trim().StartsWith("select ")) { if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { ss = "SELECT * FROM " + ss + " WHERE 1=1 {0} "; } else { ss = "SELECT * FROM [" + ss + "] WHERE 1=1 {0} "; } } StringBuilder sqlsb = new StringBuilder(); sqlsb.Append(" "); dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; int iPara = 0; foreach (DbMCControl select_control in lstDbMCControl) { if (select_control.DbMCSource == null || select_control.DbMCSource.DbMCKey != dbsource.DbMCKey || string.IsNullOrWhiteSpace(select_control.DataField) || select_control.BaseControl.MCValue == null || string.IsNullOrWhiteSpace(select_control.BaseControl.MCValue.ToString())) { continue; } if ((base.DbOptionTypesIsQuery(select_control.BaseControl))) { iPara++; string parkey = "@" + select_control.DataField + "_Para" + iPara.ToString(); string field = "[" + select_control.DataTable + "].[" + select_control.DataField + "]"; if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { parkey = ":" + select_control.DataField + "_Para" + iPara.ToString(); field = select_control.DataTable + "." + select_control.DataField; } sqlsb.Append(" AND ").Append(field).Append("=").Append(parkey); dbHelper.AddParameter(parkey, select_control.BaseControl.MCValue); } } if (iPara > 0 && ss.ToLower().Trim().IndexOf(" where ") < 0) { ss = ss + " WHERE 1=1"; } string sqlstr = string.Format(ss, sqlsb.ToString()); if (sqlstr.Length == ss.Length) { sqlstr = ss + sqlsb.ToString(); } dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); control.BaseControl.BindDataSource = dt; } } #endregion #region 再处理DataGridView控件 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(); foreach (DbMCControl control in lstDbMCControl) { if (control.DbMCSource == null || control.DbMCSource.DbMCKey != dbsource.DbMCKey || string.IsNullOrWhiteSpace(control.BaseControl.ActionDataSource)) { continue; } if (!(control.BaseControl is DataGridView)) { //跳过非DataGridView控件的处理 continue; } string ss = control.BaseControl.ActionDataSource; if (!ss.ToLower().Trim().StartsWith("select ")) { if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { ss = "SELECT * FROM " + ss + " WHERE 1=1 {0} "; } else { ss = "SELECT * FROM [" + ss + "] WHERE 1=1 {0} "; } } StringBuilder sqlsb = new StringBuilder(); sqlsb.Append(" "); dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; int iPara = 0; foreach (DbMCControl select_control in lstDbMCControl) { if (select_control.DbMCSource == null || select_control.DbMCSource.DbMCKey != dbsource.DbMCKey || string.IsNullOrWhiteSpace(select_control.DataField) || select_control.BaseControl.MCValue == null || string.IsNullOrWhiteSpace(select_control.BaseControl.MCValue.ToString())) { continue; } if ((base.DbOptionTypesIsQuery(select_control.BaseControl))) { iPara++; string parkey = "@" + select_control.DataField + "_Para" + iPara.ToString(); string field = "[" + select_control.DataTable + "].[" + select_control.DataField + "]"; if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { parkey = ":" + select_control.DataField + "_Para" + iPara.ToString(); field = select_control.DataTable + "." + select_control.DataField; } sqlsb.Append(" AND ").Append(field).Append("=").Append(parkey); dbHelper.AddParameter(parkey, select_control.BaseControl.MCValue); } } if (iPara > 0 && ss.ToLower().Trim().IndexOf("where") < 0) { ss = ss + " WHERE 1=1"; } string sqlstr = string.Format(ss, sqlsb.ToString()); if (sqlstr.Length == ss.Length) { sqlstr = ss + sqlsb.ToString(); } dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); #region 选中行处理1 int index = 0; DataGridView grid = control.BaseControl as DataGridView; if (grid != null) { foreach (DataGridViewRow row in grid.SelectedRows) { row.Selected = false; index = row.Index; } } #endregion control.BaseControl.BindDataSource = dt; #region 选中行处理2 if (grid != null) { if (grid.Rows.Count > 0 && index < grid.Rows.Count) { grid.Rows[index].Selected = true; } else if (grid.Rows.Count > 0) { grid.Rows[grid.Rows.Count - 1].Selected = true; } } #endregion string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_SynchroData_Select_msg1")); //信息查询成功,条数为{0} //ShowMsg(string.Format(msg1, dt.Rows.Count - iSubCount)); ICSharpCode.Core.LoggingService