/// <summary> /// 判断本实例更变的值与被比实体对应值是否相等 /// </summary> /// <param name="objEntity">被比较实体</param> /// <returns>是否相等</returns> public bool EqualsModify(EntityObjectBase objEntity) { if (objEntity.GetType() != this.GetType()) { return(false); } TableAttributes taEntity = objEntity.GetEntityColumns(); TableAttributes taThis = this.GetEntityColumns(); bool bEquals = true; for (int i = 0; i < taEntity.Columns.Count; i++) { ColumnAttributes caEntity = taEntity.Columns[i] as ColumnAttributes; ColumnAttributes caThis = taThis.Columns[i] as ColumnAttributes; if (caThis.IsModify && (caEntity.Value.ToString() != caThis.Value.ToString())) { bEquals = false; break; } } return(bEquals); }
/// <summary> /// 获取记录映射 /// </summary> /// <param name="objEntity">实体</param> /// <param name="tran">数据库操作事务</param> /// <returns>实体</returns> //public static EntityObjectBase Get(EntityObjectBase objEntity, SqlTransaction tran) //{ // return Get(objEntity, null, tran); //} /// <summary> /// 获取记录映射 /// </summary> /// <param name="objEntity">实体</param> /// <param name="tran">数据库操作事务</param> /// <returns>实体</returns> public static EntityObjectBase Get(EntityObjectBase objEntity, SqlTransaction tran) { TableAttributes taEntity = objEntity.GetEntityColumns(); string strKeyColumns = ""; List <SqlParameter> lstPara = new List <SqlParameter>(); for (int i = 0; i < taEntity.Columns.Count; i++) {// 组装参数 ColumnAttributes caCurrent = taEntity.Columns[i] as ColumnAttributes; if (caCurrent.IsPrimaryKey) { strKeyColumns += caCurrent.ColumnName + "=@" + caCurrent.ColumnName + " AND "; SqlParameter paraCurrent = new SqlParameter("@" + caCurrent.ColumnName, caCurrent.Value); lstPara.Add(paraCurrent); } } SqlParameter[] objPara = new SqlParameter[lstPara.Count]; for (int i = 0; i < lstPara.Count; i++) { objPara[i] = lstPara[i] as SqlParameter; } string strSql = "SELECT * FROM [TableName] with(nolock) WHERE [Key]"; // 替换当前表数据 strSql = strSql.Replace("[TableName]", taEntity.TableName); strSql = strSql.Replace("[Key]", strKeyColumns.Substring(0, strKeyColumns.Length - 5)); DataTable tbResult = null; if (null == tran) { tbResult = SqlHelper.ExecuteDataset(strSql, objPara).Tables[0]; } else { tbResult = SqlHelper.ExecuteDataset(tran, CommandType.Text, strSql, objPara).Tables[0]; } if (tbResult.Rows.Count > 0) { Type type = objEntity.GetType(); EntityObjectBase objResult = Activator.CreateInstance(type) as EntityObjectBase; objResult.FromTable(tbResult); return(objResult); } else { return(null); } }
/// <summary> /// 将指定实体修改过的值同步到本实例 /// </summary> /// <param name="objEntity">同步实体</param> public void SynchronizeModifyValue(EntityObjectBase objEntity) { TableAttributes taEntity = objEntity.GetEntityColumns(); foreach (ColumnAttributes caCurrent in taEntity.Columns) { if (true == caCurrent.IsModify) { SetMappingValue(this, caCurrent.ColumnName, caCurrent.Value); } } }
/// <summary> /// 取实体映射的数据库表信息 /// </summary> /// <param name="objEntity">实体对像</param> /// <returns>映射表信息</returns> public static TableAttributes GetEntityColumns(EntityObjectBase objEntity) { TableAttributes taEntity = new TableAttributes(); #region 取表名称 object[] objTable = objEntity.GetType().GetCustomAttributes(typeof(TableMapping), false); if (objTable.Length > 0) { if (objTable[0] is TableMapping) { TableMapping tmCurrent = objTable[0] as TableMapping; taEntity.TableName = tmCurrent.TableName; } } #endregion #region 取字段名称 foreach (PropertyInfo pi in objEntity.GetType().GetProperties()) { // 根据属性映射创建表模式并记录属性值 object[] objColumn = pi.GetCustomAttributes(typeof(ColumnMapping), false); if (objColumn.Length > 0) { if (objColumn[0] is ColumnMapping) { if (pi.CanRead) { ColumnMapping attrCurrent = objColumn[0] as ColumnMapping; ColumnAttributes caCurrent = new ColumnAttributes(); caCurrent.ColumnName = attrCurrent.ColumnName; caCurrent.IsIdentity = attrCurrent.IsIdentity; caCurrent.IsPrimaryKey = attrCurrent.IsPrimaryKey; caCurrent.IsVersionNumber = attrCurrent.IsVersionNumber; caCurrent.Value = pi.GetValue(objEntity, null); caCurrent.OriginalValue = objEntity.table.Rows[0][attrCurrent.ColumnName]; if (caCurrent.OriginalValue.ToString() != caCurrent.Value.ToString()) { caCurrent.IsModify = true; } taEntity.Columns.Add(caCurrent); } } } } #endregion return(taEntity); }
/// <summary> /// 删除记录映射 /// </summary> /// <param name="objEntity">实体</param> /// <param name="tran">数据库操作事务</param> /// <returns>影响行数</returns> //public static int Delete(EntityObjectBase objEntity, SqlTransaction tran) //{ // return Delete(objEntity, null, tran); //} /// <summary> /// 删除记录映射 /// </summary> /// <param name="objEntity">实体</param> /// <param name="conn">数据库连接</param> /// <param name="tran">数据库操作事务</param> /// <returns>影响行数</returns> public static int Delete(EntityObjectBase objEntity, SqlTransaction tran) { TableAttributes taEntity = objEntity.GetEntityColumns(); string strKeyColumns = String.Empty; List <SqlParameter> lstPara = new List <SqlParameter>(); for (int i = 0; i < taEntity.Columns.Count; i++) {// 组装参数 ColumnAttributes caCurrent = taEntity.Columns[i] as ColumnAttributes; if (caCurrent.IsPrimaryKey) { strKeyColumns += caCurrent.ColumnName + "=@" + caCurrent.ColumnName + " AND "; } else { continue; } SqlParameter paraCurrent = new SqlParameter("@" + caCurrent.ColumnName, caCurrent.Value); lstPara.Add(paraCurrent); } SqlParameter[] objPara = new SqlParameter[lstPara.Count]; for (int i = 0; i < lstPara.Count; i++) { objPara[i] = lstPara[i] as SqlParameter; } string strCondition = strKeyColumns; string strSql = "DELETE FROM [TableName] WHERE [Condition]"; // 替换当前表数据 strSql = strSql.Replace("[TableName]", taEntity.TableName); strSql = strSql.Replace("[Condition]", strCondition.Substring(0, strCondition.Length - 5)); int upCount = 0; if (null == tran) { upCount = SqlHelper.ExecuteNonQuery(strSql, objPara); } else { upCount = SqlHelper.ExecuteNonQuery(tran, CommandType.Text, strSql, objPara); } return(upCount); }
/// <summary> /// 新建记录映射 /// </summary> /// <param name="objEntity">实体</param> /// <param name="tran">数据库事务</param> /// <returns>自增字段值或影响行数</returns> public static long Create(EntityObjectBase objEntity, SqlTransaction tran) { TableAttributes taEntity = objEntity.GetEntityColumns(); string strColumns = ""; string strParaColumns = ""; bool bIsIdentity = false; List <SqlParameter> lstPara = new List <SqlParameter>(); for (int i = 0; i < taEntity.Columns.Count; i++) {// 组装参数 ColumnAttributes caCurrent = taEntity.Columns[i] as ColumnAttributes; if (caCurrent.IsIdentity) { bIsIdentity = true; continue; } strColumns += caCurrent.ColumnName + ","; strParaColumns += "@" + caCurrent.ColumnName + ","; SqlParameter paraCurrent = null; if (caCurrent.IsVersionNumber) { paraCurrent = new SqlParameter("@" + caCurrent.ColumnName, 1); } else { //创建参数 paraCurrent = new SqlParameter("@" + caCurrent.ColumnName, caCurrent.Value); } lstPara.Add(paraCurrent); } SqlParameter[] objPara = new SqlParameter[lstPara.Count]; for (int i = 0; i < lstPara.Count; i++) { objPara[i] = lstPara[i] as SqlParameter; } string strSql = "INSERT INTO [TableName]([Columns]) VALUES([ParaColumns])"; // 替换当前表数据 strSql = strSql.Replace("[TableName]", taEntity.TableName); strSql = strSql.Replace("[Columns]", strColumns.Substring(0, strColumns.Length - 1)); strSql = strSql.Replace("[ParaColumns]", strParaColumns.Substring(0, strParaColumns.Length - 1)); if (bIsIdentity) { // 带自增字段的插入 strSql += " SELECT @@IDENTITY"; if (null == tran) { return(long.Parse(SqlHelper.ExecuteScalar(strSql, objPara).ToString())); } else { return(long.Parse(SqlHelper.ExecuteScalar(tran, CommandType.Text, strSql, objPara).ToString())); } } else { // 不带自增字段的插入 if (null == tran) { return(SqlHelper.ExecuteNonQuery(strSql, objPara)); } else { return(SqlHelper.ExecuteNonQuery(tran, CommandType.Text, strSql, objPara)); } } }