/// <summary> /// 创建用于删除的Sql命令 /// </summary> public static DbCommand CreatDeleteCommand <T>(Database db, T entity) { Type entityType = typeof(T); List <string> primaryKeyEntityFieldNames = EntityMappingTool.GetPrimaryKeyOfEntityField(entityType); List <string> primaryKeyDbCloumnNames = EntityMappingTool.GetDbColumnNames(entityType, primaryKeyEntityFieldNames); List <DbType> primaryKeyDbColumnTypes = EntityMappingTool.GetDbColumnTypes(entityType, primaryKeyEntityFieldNames); List <PropertyInfo> primaryKeyPropertyInfos = EntityMappingTool.GetEntityPropertyInfos(entityType, primaryKeyEntityFieldNames); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.AppendFormat("DELETE FROM [{0}] WHERE ", EntityMappingTool.GetDbTableName(entityType)); for (int i = 0; i < primaryKeyEntityFieldNames.Count; i++) { sqlBuilder.Append((i > 0) ? " AND " : ""); sqlBuilder.AppendFormat("([{0}]=@{0})", primaryKeyDbCloumnNames[i]); } //参数 DbCommand cmd = db.GetSqlStringCommand(sqlBuilder.ToString()); for (int i = 0; i < primaryKeyEntityFieldNames.Count; i++) { db.AddInParameter(cmd, "@" + primaryKeyDbCloumnNames[i], primaryKeyDbColumnTypes[i], primaryKeyPropertyInfos[i].GetValue(entity, null)); } return(cmd); }
/// <summary> /// 判断Entity实例中有没有设置主键值 /// </summary> public static bool HasPrimaryKeyValue <T>(T entity) { if (!EntityMappingTool.HasPrimaryKey(typeof(T))) { return(false); } List <string> notNullEntityFields = GetNotNullFields(entity); if (notNullEntityFields == null) { return(false); } var primaryKeyEntityFieldNames = EntityMappingTool.GetPrimaryKeyOfEntityField(typeof(T)); for (int i = 0; i < primaryKeyEntityFieldNames.Count; i++) { if (!notNullEntityFields.Any(n => n == primaryKeyEntityFieldNames[i])) { return(false); } } return(true); }
/// <summary> /// 生成用于更新的Sql命令 /// </summary> public static DbCommand CreateUpdateCommand <T>(Database db, T entity) { Type entityType = typeof(T); List <string> primaryKeyEntityFieldNames = EntityMappingTool.GetPrimaryKeyOfEntityField(entityType); List <string> primaryKeyDbCloumnNames = EntityMappingTool.GetDbColumnNames(entityType, primaryKeyEntityFieldNames); List <DbType> primaryKeyDbColumnTypes = EntityMappingTool.GetDbColumnTypes(entityType, primaryKeyEntityFieldNames); List <string> notNullEntityFields = EntityInstanceTool.GetNotNullFields(entity); List <string> notNullDbCloumnNames = EntityMappingTool.GetDbColumnNames(entityType, notNullEntityFields); List <DbType> notNullDbColumnTypes = EntityMappingTool.GetDbColumnTypes(entityType, notNullEntityFields); List <PropertyInfo> notNullEntityPropertys = EntityInstanceTool.GetNotNullEntityPropertys(entity); //生成Sql语句 List <string> parameterIndex = new List <string>(); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append("UPDATE [").Append(EntityMappingTool.GetDbTableName(entityType)).Append("] SET "); bool firstColumn = true; for (int i = 0; i < notNullDbCloumnNames.Count; i++) { string loopColumn = notNullDbCloumnNames[i]; //当前模式主键不更新 if (primaryKeyDbCloumnNames.Contains(loopColumn)) { continue; } sqlBuilder.Append(firstColumn ? "" : ","); firstColumn = false; sqlBuilder.AppendFormat("[{0}]=@{0}", loopColumn); parameterIndex.Add(loopColumn); } //WHERE sqlBuilder.Append(" WHERE "); for (int i = 0; i < primaryKeyDbCloumnNames.Count; i++) { sqlBuilder.Append((i > 0) ? " AND " : ""); sqlBuilder.AppendFormat("([{0}]=@{0})", primaryKeyDbCloumnNames[i]); parameterIndex.Add(primaryKeyDbCloumnNames[i]); } DbCommand cmd = db.GetSqlStringCommand(sqlBuilder.ToString()); for (int i = 0; i < parameterIndex.Count; i++) { int pIndex = notNullDbCloumnNames.IndexOf(parameterIndex[i]); db.AddInParameter(cmd, "@" + notNullDbCloumnNames[pIndex], notNullDbColumnTypes[pIndex], notNullEntityPropertys[pIndex].GetValue(entity, null)); } return(cmd); }