using System; using System.Collections.Generic; using System.Linq; using System.Text; using Mesnac.Controls.Base; using System.Data; using System.Data.SqlClient; using System.Data.Common; using Mesnac.Codd.Session; using Mesnac.Basic; using Mesnac.Gui.Edit.Global; using System.ComponentModel; namespace Mesnac.Action.Base { #region 数据库操作控件基本信息 /// /// 软控数据库控件命名解析 /// public class DbMCControlDesignKeyConfig { /// /// 解析命名 /// /// /// public string[] SplitKey(string ss) { List Result = new List(); if (!string.IsNullOrWhiteSpace(ss)) { string[] arr = ss.Split(new string[] { "].[" }, StringSplitOptions.RemoveEmptyEntries); foreach (string a in arr) { string str = a.Trim(); if (str.StartsWith("[")) { str = str.Substring(1); } if (str.EndsWith("]")) { str = str.Substring(0, str.Length - 1); } Result.Add(str); } } return Result.ToArray(); } /// /// 组合命名 /// /// /// public string ToString(string[] ss) { StringBuilder sb = new StringBuilder("["); foreach (string s in ss) { if (!string.IsNullOrWhiteSpace(s)) { sb.Append(s.Trim()).Append("].["); } } if (sb.Length > 2) { sb.Remove(sb.Length - 2, 2); return sb.ToString(); } else { return string.Empty; } } } /// /// 软控不可见控件 MCDataSource 的解析封装 /// public class DbMCSource { public DbMCSource(MCDataSource source) { this.MCDataSource = source; string[] dbInfo_dbsource = new DbMCControlDesignKeyConfig().SplitKey(source.TableName.ToString()); if ((dbInfo_dbsource != null) && (dbInfo_dbsource.Length >= 2)) { this.DesignSource = dbInfo_dbsource[0]; this.DataTable = dbInfo_dbsource[1]; } if (!string.IsNullOrWhiteSpace(this.DesignSource)) { DataSourceItem item; if (DataSourceFactory.Instance.DataSources.TryGetValue(this.DesignSource, out item)) { this.DataSourceItem = item; } } } /// /// 不可见控件MCDataSource /// public MCDataSource MCDataSource { get; private set; } /// /// 数据连接信息 /// public DataSourceItem DataSourceItem { get; private set; } /// /// 数据源名称 /// public string DesignSource { get; private set; } /// /// 表名 /// public string DataTable { get; private set; } /// /// 标准名称 /// public string DbMCKey { get { DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig(); return config.ToString(new string[] { this.DesignSource, this.DataTable }); } } } /// /// 软控控件 的解析封装 /// public class DbMCControl { public DbMCControl(DbMCSource source, IBaseControl control) { this.BaseControl = control; this.DbMCSource = source; string[] dbInfo_control = new DbMCControlDesignKeyConfig().SplitKey(control.MCKey); if (source != null) { this.DesignSource = source.DesignSource; this.DataTable = source.DataTable; if (dbInfo_control.Length > 1) { if ((dbInfo_control[0].Equals(source.DesignSource, StringComparison.CurrentCultureIgnoreCase)) && (dbInfo_control[1].Equals(source.DataTable, StringComparison.CurrentCultureIgnoreCase))) { if (dbInfo_control.Length > 2) { this.DataField = dbInfo_control[2]; } } } else if (dbInfo_control.Length > 0) { this.DataField = dbInfo_control[0]; } } else if (dbInfo_control.Length >= 2) { this.DesignSource = dbInfo_control[0]; this.DataTable = dbInfo_control[1]; if (dbInfo_control.Length > 2) { this.DataField = dbInfo_control[2]; } } if (!string.IsNullOrWhiteSpace(this.DesignSource)) { DataSourceItem item; if (DataSourceFactory.Instance.DataSources.TryGetValue(this.DesignSource, out item)) { this.DataSourceItem = item; } } } /// /// 软控控件 /// public IBaseControl BaseControl { get; private set; } /// /// 软控控件数据源 /// public DbMCSource DbMCSource { get; set; } /// /// 数据连接信息 /// public DataSourceItem DataSourceItem { get; private set; } /// /// 数据源名称 /// public string DesignSource { get; private set; } /// /// 表名 /// public string DataTable { get; private set; } /// /// 字段名 /// public string DataField { get; private set; } /// /// 标准名称 /// public string DbMCKey { get { DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig(); return config.ToString(new string[] { this.DesignSource, this.DataTable, this.DataField }); } } } #endregion /// /// 数据操作类 /// public class DatabaseAction : BaseAction { #region RunIni /// /// 接口方法 /// /// public new void RunIni(RuntimeParameter runtime) { base.RunIni(runtime); //必须调用 } #endregion #region GetAllDbMCSources private List getAllDbMCSources(List clist) { List Result = new List(); foreach (Component component in clist) { if ((component is MCDataSource) && (!string.IsNullOrWhiteSpace(component.Site.Name))) { Result.Add(new DbMCSource(component as MCDataSource)); } } return Result; } protected List GetThisFormAllDbMCSources() { return getAllDbMCSources(GetThisFormAllComponents()); } protected List GetAllFormAllDbMCSources() { return getAllDbMCSources(GetAllFormAllComponents()); } protected List GetThisOrAllFormAllDbMCSources() { List Result = GetThisFormAllDbMCSources(); if (Result.Count > 0) { return Result; } return GetAllFormAllDbMCSources(); } public List GetAllDbMCSources() { return GetThisFormAllDbMCSources(); } #endregion #region GetAllDbMCControls private List getAllDbMCControls(List lstDbMCSource, List lstControl) { List Result = new List(); foreach (IBaseControl control in lstControl) { if (!control.IsDbControl) { continue; } //初始化MCDataSource属性 DbMCSource source = null; if (!string.IsNullOrWhiteSpace(control.MCDataSourceID)) { foreach (DbMCSource component in lstDbMCSource) { if (component.MCDataSource.Equals(control.MCDataSource)) { source = component; break; } } } Result.Add(new DbMCControl(source, control)); } foreach (DbMCControl control in Result) { if (control.DbMCSource != null) { continue; } foreach (DbMCSource dbsource in lstDbMCSource) { if ((control.DesignSource == dbsource.DesignSource) && (control.DataTable == dbsource.DataTable)) { control.DbMCSource = dbsource; } } } return Result; } protected List GetThisFormAllDbMCControls() { return getAllDbMCControls(GetThisFormAllDbMCSources(), GetThisFormAllMCControls()); } protected List GetAllFormAllDbMCControls() { return getAllDbMCControls(GetAllFormAllDbMCSources(), GetAllFormAllMCControls()); } protected List GetThisOrAllFormAllDbMCControls() { List Result = GetThisFormAllDbMCControls(); if (Result.Count > 0) { return Result; } return GetAllFormAllDbMCControls(); } public List GetAllDbMCControls() { return GetThisFormAllDbMCControls(); } #endregion #region GetAllDbMCControlsByOption private List getAllDbMCControlsByOption(DbOptionTypes dbopttype, List lstDbMCSource, List lstControl) { List Result = new List(); foreach (IBaseControl control in lstControl) { if (!(control is IBaseControl) || control.DbOptionType != dbopttype) { continue; } //初始化MCDataSource属性 DbMCSource source = null; if (!string.IsNullOrWhiteSpace(control.MCDataSourceID)) { foreach (DbMCSource component in lstDbMCSource) { if (component.MCDataSource.Site.Name == control.MCDataSourceID) { source = component; break; } } } Result.Add(new DbMCControl(source, control)); } foreach (DbMCControl control in Result) { if (control.DbMCSource != null) { continue; } foreach (DbMCSource dbsource in lstDbMCSource) { if ((control.DesignSource == dbsource.DesignSource) && (control.DataTable == dbsource.DataTable)) { control.DbMCSource = dbsource; } } } return Result; } protected List GetThisFormAllDbMCControlsByOption(DbOptionTypes dbopttype) { return getAllDbMCControlsByOption(dbopttype, GetThisFormAllDbMCSources(), GetThisFormAllMCControls()); } protected List GetAllFormAllDbMCControlsByOption(DbOptionTypes dbopttype) { return getAllDbMCControlsByOption(dbopttype, GetAllFormAllDbMCSources(), GetAllFormAllMCControls()); } protected List GetThisOrAllFormAllDbMCControlsByOption(DbOptionTypes dbopttype) { List Result = GetThisFormAllDbMCControlsByOption(dbopttype); if (Result.Count > 0) { return Result; } return GetAllFormAllDbMCControlsByOption(dbopttype); } public List GetAllDbMCControlsByOption(DbOptionTypes dbopttype) { return GetThisFormAllDbMCControlsByOption(dbopttype); } #endregion #region GetDbMCControlByKey private List getDbMCControlByKey(string key, List lstDbMCControl) { List Result = new List(); DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig(); string[] dbInfo_control = config.SplitKey(key); if (dbInfo_control.Length > 2) { key = config.ToString(dbInfo_control); } foreach (DbMCControl control in lstDbMCControl) { if (control.DbMCKey != null && control.DbMCKey.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { Result.Add(control); } } return Result; } protected List GetThisFormDbMCControlByKey(string key) { return getDbMCControlByKey(key,GetThisFormAllDbMCControls()); } protected List GetAllFormDbMCControlByKey(string key) { return getDbMCControlByKey(key, GetAllFormAllDbMCControls()); } protected List GetThisOrAllFormDbMCControlByKey(string key) { List Result = GetThisFormDbMCControlByKey(key); if (Result.Count > 0) { return Result; } return GetAllFormDbMCControlByKey(key); } public List GetDbMCControlByKey(string key) { return GetThisOrAllFormDbMCControlByKey(key); } #endregion #region GetDbMCControlByKey private List getDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key, List lstControl) { List Result = new List(); DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig(); string[] dbInfo_keys = config.SplitKey(key); string[] dbInfo_control = new string[dbInfo_keys.Length + 1]; dbInfo_control[0] = Mesnac.Basic.DataSourceFactory.Instance.GetDataSourceName(dbType); for (int i = 0; i < dbInfo_keys.Length; i++) { dbInfo_control[i + 1] = dbInfo_keys[i]; } key = config.ToString(dbInfo_control); foreach (DbMCControl control in lstControl) { if (control.DbMCKey != null && control.DbMCKey.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { Result.Add(control); } } return Result; } protected List GetThisFomrDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key) { return getDbMCControlByKey(dbType,key,GetThisFormAllDbMCControls()); } protected List GetAllFomrDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key) { return getDbMCControlByKey(dbType, key, GetAllFormAllDbMCControls()); } protected List GetThisOrAllFomrDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key) { List Result = GetThisFomrDbMCControlByKey(dbType, key); if (Result.Count > 0) { return Result; } return GetAllFomrDbMCControlByKey(dbType, key); } public List GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key) { return GetThisOrAllFomrDbMCControlByKey(dbType, key); } #endregion #region GetDbMCControlByBaseControlKey private List getDbMCControlByBaseControlKey(string key, List lstDbMCControl) { List Result = new List(); foreach (DbMCControl control in lstDbMCControl) { if (control.BaseControl.MCKey != null && control.BaseControl.MCKey.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { Result.Add(control); } } return Result; } protected List GetThisFormDbMCControlByBaseControlKey(string key) { return getDbMCControlByBaseControlKey(key,GetThisFormAllDbMCControls()); } protected List GetAllFormDbMCControlByBaseControlKey(string key) { return getDbMCControlByBaseControlKey(key, GetAllFormAllDbMCControls()); } protected List GetThisOrAllFormDbMCControlByBaseControlKey(string key) { List Result = GetThisFormDbMCControlByBaseControlKey(key); if (Result.Count > 0) { return Result; } return GetAllFormDbMCControlByBaseControlKey(key); } public List GetDbMCControlByBaseControlKey(string key) { return GetThisFormDbMCControlByBaseControlKey(key); } public List GetComponentContral(string key) { List list = new List(); foreach (Component c in GetAllFormAllComponents()) { if (c.Site.Name == key) { list.Add(c as IBaseControl); } } return list; } #endregion #region 数据库操作 #region 获取数据源组 /// /// 获取数据源组 /// /// /// public DataSourceItem GetDataSourceItem(Mesnac.Basic.DataSourceFactory.MCDbType dbType) { return DataSourceFactory.Instance.GetDataSourceItem(dbType); } #endregion #region NewDbHelper /// /// 新建数据库操作类 /// /// /// private DbHelper NewDbHelper(DataSourceItem item) { return DataSourceFactory.Instance.GetDbHelper(item); } public DbHelper NewDbHelper(string dbName) { return DataSourceFactory.Instance.GetDbHelper(dbName); } public DbHelper NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType dbType) { return DataSourceFactory.Instance.GetDbHelper(dbType); } #endregion #region 同步数据库 /// /// 同步表 /// public void SynchroTable() { foreach (DbMCSource dbsource in GetThisFormAllDbMCSources()) { if (string.IsNullOrWhiteSpace(dbsource.DesignSource)) { continue; } if (string.IsNullOrWhiteSpace(dbsource.DataTable.ToString())) { continue; } string source = dbsource.DesignSource; string table = dbsource.DataTable; DbHelper dbHelper = NewDbHelper(source); if (dbHelper == null) { continue; } string sqlstr = "SELECT name FROM sysobjects WHERE name = N'" + table + "' AND type = 'U'"; if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { sqlstr = "select TNAME as name from tab where TNAME='"+ table + "' and TABTYPE='TABLE'"; } else if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OleDbFactory")) { sqlstr = "SELECT MSysObjects.Name as name FROM MsysObjects WHERE (Left([Name],1)<>'~') AND (Left$([Name],4) <> 'Msys') AND (MSysObjects.Type)=1 and MSysObjects.Name='" + table + "'"; } dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); if (dt.Rows.Count < 1) { //创建表 sqlstr = @"CREATE TABLE [" + table + @"]( [ObjID] INT IDENTITY(1,1) NOT NULL, CONSTRAINT PK_ObjID_IN" + table + @"_IDENTITY PRIMARY KEY CLUSTERED([ObjID] ASC) );"; if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { sqlstr = @"CREATE SEQUENCE seq_{0}; CREATE TABLE {0}(ObjID INT primary key); CREATE OR REPLACE TRIGGER tri_{0}_insert before insert on userinfo for each row declare begin select seq_{0}.nextval into :NEW.ObjID from dual; end tri_userInfo_insert;"; sqlstr = String.Format(sqlstr, table); } else if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OleDbFactory")) { sqlstr = @"CREATE TABLE {0}(ObjID INT IDENTITY(1,1) primary key)"; sqlstr = String.Format(sqlstr, table); } dbHelper.CommandText = sqlstr; dbHelper.ExecuteNonQuery(); } } } /// /// 同步字段 /// public void SynchroField() { foreach (DbMCControl control in GetThisFormAllDbMCControls()) { if ((control.BaseControl.IsDbControl) && (!string.IsNullOrWhiteSpace(control.DesignSource)) && (!string.IsNullOrWhiteSpace(control.DataTable)) && (!string.IsNullOrWhiteSpace(control.DataField)) ) { DbHelper dbHelper = NewDbHelper(control.DesignSource); if (dbHelper == null) { continue; } //查询表 string sqlstr = String.Empty; sqlstr = "select * from [" + control.DataTable + "] where 1=2"; if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { sqlstr = "select * from {0} where 1=2"; sqlstr = String.Format(sqlstr, control.DataTable); } dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); bool isExists = false; foreach (DataColumn dc in dt.Columns) { if (control.DataField.Equals(dc.ColumnName, StringComparison.CurrentCultureIgnoreCase)) { isExists = true; break; } } if (!isExists) { //创建字段 sqlstr = "ALTER TABLE [" + control.DataTable + "] ADD " + control.DataField + " ntext NULL"; if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory")) { sqlstr = "alter table {0} add {1} varchar2(500) null"; sqlstr = String.Format(sqlstr, control.DataTable, control.DataField); } dbHelper.CommandText = sqlstr; dbHelper.ExecuteNonQuery(); } } } } #endregion #region 执行存储过程的方法 /// /// 获取存储过程执行器 /// /// 数据库类型,网络库、本地库 /// 存储过程名称 /// 参数列表 /// 返回存储过程执行器 public DbHelper GetProcedureHelper(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string procedureName, Dictionary parameters) { if (!String.IsNullOrEmpty(procedureName)) { DbHelper dbHelper; dbHelper = this.NewDbHelper(dbType); if (dbHelper == null) { base.LogError("获取数据连接失败..."); return null; } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.StoredProcedure; dbHelper.CommandText = procedureName; if (parameters != null && parameters.Keys.Count > 0) { foreach (string key in parameters.Keys) { dbHelper.AddParameter(key, parameters[key]); } } return dbHelper; } return null; } /// /// 执行存储过程 /// /// 数据库类型,网络库、本地库 /// 存储过程名称 /// 参数列表 public void ExecuteProcedure(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string procedureName, Dictionary parameters) { DbHelper dbHelper = this.GetProcedureHelper(dbType, procedureName, parameters); if (dbHelper != null) { dbHelper.ExecuteNonQuery(); } } #endregion #endregion } }