public bool CreateTables(DalLayerInfo dalInfo, Type[] entityTypes) { List <string> sqls = new List <string>(); object[] customAttributes; Type attType = typeof(EntityAttribute); foreach (Type item in entityTypes) { bool isEntity = false; customAttributes = item.GetCustomAttributes(false); if (customAttributes != null) { foreach (var a in customAttributes) { if (a.GetType() == attType) { isEntity = true; break; } } } if (!isEntity) { continue; } sqls.Add(dalInfo.Provider.GetTableScript(item)); } return(SqlHelper.ExecuteTransac(dalInfo.Connection, dalInfo.LongConnection, sqls)); }
/// <summary> /// 构造方法 /// </summary> public TransacBuilder(DalLayerInfo dalInfo, Dictionary <string, string> dicKeyNames, Dictionary <string, string> dicIdentityNames) { this.dalInfo = dalInfo; this.dicKeyNames = dicKeyNames; this.dicIdentityNames = dicIdentityNames; this.commandTypes = new List <CommandType>(); this.cmdTexts = new List <string>(); this.cmdParams = new List <DbParameter[]>(); }
private void SetBuilderParams(DalLayerInfo dalInfo, string keyName, TransacBuilder tranBuilder) { this.dalInfo = dalInfo; this.keyName = keyName; if (tranBuilder != null) { this.tranBuilder = tranBuilder; this.tranBuilder.BeforeRun += this.CreateSqlCommand; } }
public void BulkCopy(string SqlCommand, BulkCopyHandler bulkCopy) { DalLayerInfo dalInfo = GetDalyerInfo(); DbConnection conntion = dalInfo.Connection; if (!dalInfo.LongConnection) { conntion.Open(); } using (System.Data.SqlServerCe.SqlCeCommand comm = (System.Data.SqlServerCe.SqlCeCommand)conntion.CreateCommand()) { comm.CommandText = SqlCommand; comm.CommandType = CommandType.Text; System.Data.SqlServerCe.SqlCeResultSet ceRsSet = comm.ExecuteResultSet(System.Data.SqlServerCe.ResultSetOptions.Scrollable | System.Data.SqlServerCe.ResultSetOptions.Updatable); try { System.Data.SqlServerCe.SqlCeUpdatableRecord upRs = ceRsSet.CreateRecord(); if (bulkCopy != null) { bulkCopy(ceRsSet, upRs); } } catch (Exception ex) { throw ex; } finally { ceRsSet.Close(); } } if (!dalInfo.LongConnection) { if (conntion.State != ConnectionState.Closed) { conntion.Close(); } conntion.Dispose(); } }
public void SyncTables(ITransacBuilder transacBuilder, DalLayerInfo dalInfo, Type[] entityTypes) { List <string> sqls = new List <string>(); object[] customAttributes; Type attType = typeof(EntityAttribute); foreach (Type item in entityTypes) { bool isEntity = false; customAttributes = item.GetCustomAttributes(false); if (customAttributes != null) { foreach (var a in customAttributes) { if (a.GetType() == attType) { isEntity = true; break; } } } if (!isEntity) { continue; } List <string> tempSql = dalInfo.Provider.GetNewColumnsCommandTexts(dalInfo, item); if (tempSql != null && tempSql.Count > 0) { sqls.AddRange(tempSql); } } foreach (var item in sqls) { transacBuilder.AddCommandText(item); } }
public List <string> GetNewColumnsCommandTexts(DalLayerInfo dalInfo, Type entityType) { List <string> list = new List <string>(); string tableName = entityType.Name; DataTable dt = null; // 检查表是否存在 string sqlCheck = string.Format("SELECT TOP 1 * FROM {0};", tableName); try { dt = SqlHelper.ExecuteDataTable(dalInfo.Connection, dalInfo.Provider.CreateDataAdapter(), dalInfo.LongConnection, sqlCheck, null); } catch (Exception ex) { } if (dt == null) { // 表不存在 list.Add(GetTableScript(entityType)); return(list); } // 如果表已存在,则检查字段是否完整(通过字段名称判断) List <string> dtColumns = new List <string>(); foreach (DataColumn column in dt.Columns) { dtColumns.Add(column.ColumnName.ToLower().Trim()); } bool primaryKey; bool identity; object length; string dataType; bool arrowNull; object[] objs; PropertyInfo[] pinfos = entityType.GetProperties(); StringBuilder sb = new StringBuilder(); foreach (PropertyInfo pinfo in pinfos) { if (dtColumns.Contains(pinfo.Name.ToLower().Trim())) { continue; } sb.Remove(0, sb.Length); primaryKey = false; identity = false; length = null; dataType = null; arrowNull = true; objs = pinfo.GetCustomAttributes(false); if (objs != null && objs.Length > 0) { EntityAttribute cusatt = (EntityAttribute)objs[0]; if (cusatt.DisplayOnly) { continue; } if (cusatt.PrimaryKey) { primaryKey = true; arrowNull = false; } if (cusatt.Identity) { identity = true; arrowNull = false; } length = cusatt.Length; dataType = cusatt.DataType; } sb.Append("alter table "); sb.Append(tableName); sb.Append(" add "); sb.Append(pinfo.Name); sb.Append(" "); sb.Append(GetColumnType(pinfo, dataType, length)); if (arrowNull) { if (pinfo.PropertyType.IsGenericType) { if (pinfo.PropertyType.GetGenericTypeDefinition() != typeof(Nullable <>)) { arrowNull = false; } } } if (!arrowNull) { sb.Append(" not null"); sb.Append(" default 0"); } else { sb.Append(" null"); } if (identity) { sb.Append(" identity(8,1)"); } list.Add(sb.ToString()); if (primaryKey) { sb.Remove(0, sb.Length); sb.Append("alter table "); sb.Append(tableName); sb.Append(" add constraint"); sb.Append(" pk_"); sb.Append(tableName); sb.Append("_"); sb.Append(pinfo.Name); sb.Append(" primary key("); sb.Append(pinfo.Name); sb.Append(")"); list.Add(sb.ToString()); } } return(list); }
internal QueryBuilder(DalLayerInfo dalInfo, string keyName) { this.dalInfo = dalInfo; this.keyName = keyName; }
internal InsertBuilder(DalLayerInfo dalInfo, string keyName, string identityName, TransacBuilder tranBuilder) { SetBuilderParams(dalInfo, keyName, identityName, tranBuilder); }
internal InsertBuilder(DalLayerInfo dalInfo, string keyName, string identityName) { SetBuilderParams(dalInfo, keyName, identityName, null); }
internal UpdateBuilder(DalLayerInfo dalInfo, string keyName, TransacBuilder tranBuilder) { SetBuilderParams(dalInfo, keyName, tranBuilder); }
internal UpdateBuilder(DalLayerInfo dalInfo, string keyName) { SetBuilderParams(dalInfo, keyName, null); }
/// <summary> /// 初始化数据库 /// </summary> /// <returns></returns> public bool Init(ref string msg) { try { this.initThreadId = Thread.CurrentThread.ManagedThreadId; this.dalInfo = new DalLayerInfo(this.connectionString, this.longConnection, this.provider); // 测试数据库连接 if (string.IsNullOrEmpty(connectionString.Trim())) { msg = "连接字符串为空"; return(false); } object obj = SqlHelper.ExecuteScalar(provider.CreateConnection(connectionString), false, "select 1"); if (obj == null || obj == DBNull.Value) { return(false); } if (Convert.ToInt32(obj) != 1) { return(false); } // 获取表主键及自增字段 object[] objs; object[] customAttributes; Type attType = typeof(EntityAttribute); foreach (Type entityType in this.entityTypes) { bool isEntity = false; customAttributes = entityType.GetCustomAttributes(false); if (customAttributes != null) { foreach (var a in customAttributes) { if (a.GetType() == attType) { isEntity = true; break; } } } if (!isEntity) { continue; } // 找到目标次数 int findCount = 0; foreach (PropertyInfo pinfo in entityType.GetProperties()) { findCount = 0; objs = pinfo.GetCustomAttributes(false); if (objs != null && objs.Length > 0) { EntityAttribute cusatt = (EntityAttribute)objs[0]; if (cusatt.PrimaryKey) { dicKeyNames.Add(entityType.Name, pinfo.Name); findCount++; } if (cusatt.Identity) { dicIdentityNames.Add(entityType.Name, pinfo.Name); findCount++; } if (findCount == 2) { break; } } } } return(true); } catch (Exception ex) { msg = ex.Message; return(false); } }
/// <summary> /// 初始化数据库 /// </summary> /// <returns></returns> public bool Init() { this.initThreadId = Thread.CurrentThread.ManagedThreadId; // 是否需要创建数据库 bool isCreate = false; // 是否需要升级数据库 bool isUpdate = false; // 获取配置文件版本号 string dbDir = Common.GetDir(dbPath); int dbOldVersion = VersionManage.GetVersion(this.dbMark, dbDir); if (!File.Exists(dbPath)) { isCreate = true; } else if (File.Exists(dbPath)) { if (dbOldVersion < dbVersion) { isUpdate = true; } } // 获取表主键及自增字段 object[] objs; object[] customAttributes; Type attType = typeof(EntityAttribute); foreach (Type entityType in this.entityTypes) { bool isEntity = false; customAttributes = entityType.GetCustomAttributes(false); if (customAttributes != null) { foreach (var a in customAttributes) { if (a.GetType() == attType) { isEntity = true; break; } } } if (!isEntity) { continue; } // 找到目标次数 int findCount = 0; foreach (PropertyInfo pinfo in entityType.GetProperties()) { findCount = 0; objs = pinfo.GetCustomAttributes(false); if (objs != null && objs.Length > 0) { EntityAttribute cusatt = (EntityAttribute)objs[0]; if (cusatt.PrimaryKey) { dicKeyNames.Add(entityType.Name, pinfo.Name); findCount++; } if (cusatt.Identity) { dicIdentityNames.Add(entityType.Name, pinfo.Name); findCount++; } if (findCount == 2) { break; } } } } string connectionString = provider.GetConnectionString(dbPath, password); this.dalInfo = new DalLayerInfo(connectionString, this.longConnection, this.provider); if (isCreate) { // 新建数据库 DBHelper dbHelper = new DBHelper(); provider.CreateDatabase(dbPath, password); try { Open(); // 创建表 dbHelper.CreateTables(dalInfo, this.entityTypes); // 触发事件 if (this.createdDatabase != null) { this.createdDatabase(); } Close(); if (dbOldVersion != dbVersion) { VersionManage.SetVersion(this.dbMark, dbDir, dbVersion); } } catch (Exception ex) { // 如果执行不成功则删除新建的数据库 if (File.Exists(dbPath)) { Close(); try { File.Delete(dbPath); } catch { } } throw ex; } } else if (isUpdate) { // 升级数据库 Open(); if (this.upgradeDatabase != null) { this.upgradeDatabase(dbOldVersion, dbVersion); } Close(); if (dbOldVersion != dbVersion) { VersionManage.SetVersion(this.dbMark, dbDir, dbVersion); } } return(true); }