/// <summary>
        /// 把传输对像转换成数据表
        /// </summary>
        /// <param name="objTranfser">数据传输对像</param>
        /// <returns>转换得到的表</returns>
        private DataTable MappingToTable(EntityObjectBase objTranfser)
        {
            ArrayList alRow = new ArrayList();

            DataTable dt = new DataTable(objTranfser.GetType().Name);

            foreach (PropertyInfo pi in objTranfser.GetType().GetProperties())
            { // 根据属性映射创建表模式并记录属性值
                object[] obj = pi.GetCustomAttributes(typeof(ColumnMapping), false);
                if (obj.Length > 0)
                {
                    if (obj[0] is ColumnMapping)
                    {
                        ColumnMapping tmAtrr = obj[0] as ColumnMapping;

                        if (pi.CanRead && !dt.Columns.Contains(tmAtrr.ColumnName))
                        {
                            dt.Columns.Add(tmAtrr.ColumnName);         // 创建表例

                            alRow.Add(pi.GetValue(objTranfser, null)); // 记录属性值
                        }
                    }
                }
            }

            dt.Rows.Add(alRow.ToArray());

            return(dt);
        }
        /// <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);
        }
Beispiel #3
0
        /// <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="objTranfser">被映射的传输对像</param>
        private void MappingToObject(EntityObjectBase objTranfser)
        {
            DataRow row = this.table.Rows[0];

            foreach (DataColumn col in this.table.Columns)
            {
                if (row[col] != System.DBNull.Value)
                {
                    this.SetMappingValue(objTranfser, col.ColumnName, row[col]);
                }
            }
        }
        /// <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);
        }
        public static EntityObjectBase Copy(EntityObjectBase objEntity)
        {
            //生成拷贝实例
            Type             type    = objEntity.GetType();
            EntityObjectBase objCopy = (EntityObjectBase)Activator.CreateInstance(type);

            //拷贝属性值
            objCopy.FromTable(objEntity.ToTable());

            //拷贝原始值
            objCopy.table = objEntity.table.Copy();

            return(objCopy);
        }
Beispiel #8
0
        /// <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="objForgetKeyEntity">从表实体</param>
        /// <param name="objValue">主键值</param>
        public void SetForgetKeyValue(EntityObjectBase objForgetKeyEntity, object objValue)
        {
            Type   type            = this.GetType();
            string strKeyCoumnName = "";

            foreach (PropertyInfo pi in  type.GetProperties())
            {
                object[] obj = pi.GetCustomAttributes(typeof(ColumnMapping), false);
                if (obj.Length > 0)
                {
                    if (obj[0] is ColumnMapping)
                    {
                        ColumnMapping tmAtrr = obj[0] as ColumnMapping;
                        if (true == tmAtrr.IsPrimaryKey)
                        {
                            pi.SetValue(this, Convert.ChangeType(objValue, pi.PropertyType), null);
                            strKeyCoumnName = tmAtrr.ColumnName;
                            break;
                        }
                    }
                }
            }

            type = objForgetKeyEntity.GetType();
            foreach (PropertyInfo pi in  type.GetProperties())
            {
                object[] obj = pi.GetCustomAttributes(typeof(ColumnMapping), false);
                if (obj.Length > 0)
                {
                    if (obj[0] is ColumnMapping)
                    {
                        ColumnMapping tmAtrr = obj[0] as ColumnMapping;
                        if (strKeyCoumnName == tmAtrr.ColumnName)
                        {
                            pi.SetValue(objForgetKeyEntity, Convert.ChangeType(objValue, pi.PropertyType), null);
                            break;
                        }
                    }
                }
            }
        }
 /// <summary>
 /// 设置原始值
 /// </summary>
 /// <param name="objEntity">实体对像</param>
 public static void SetOriginalValue(EntityObjectBase objEntity)
 {
     objEntity.table = objEntity.ToTable();
 }
Beispiel #11
0
 /// <summary>
 /// 获取记录映射
 /// </summary>
 /// <param name="objEntity">实体</param>
 /// <param name="conn">数据库连接</param>
 /// <returns>实体</returns>
 public static EntityObjectBase Get(EntityObjectBase objEntity)
 {
     return(Get(objEntity, null));
 }
Beispiel #12
0
 /// <summary>
 /// 删除记录映射
 /// </summary>
 /// <param name="objEntity">实体</param>
 /// <param name="conn">数据库连接</param>
 /// <returns>影响行数</returns>
 public static int Delete(EntityObjectBase objEntity)
 {
     return(Delete(objEntity, null));
 }
Beispiel #13
0
 /// <summary>
 /// 更新记录映射
 /// </summary>
 /// <param name="objEntity">实体</param>
 /// <param name="conn">数据库连接</param>
 /// <returns>影响的行数</returns>
 public static int Update(EntityObjectBase objEntity)
 {
     return(Update(objEntity, null));
 }
Beispiel #14
0
        /// <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));
                }
            }
        }
Beispiel #15
0
        /// <summary>
        /// 新建记录映射
        /// </summary>
        /// <param name="objEntity">实体</param>
        /// <param name="conn">数据库连接</param>
        /// <returns>自增字段值或影响行数</returns>
        //public static long Create(EntityObjectBase objEntity,SqlConnection conn)
        //{
        //    return Create(objEntity,conn,null);
        //}

        /// <summary>
        /// 新建记录映射
        /// </summary>
        /// <param name="objEntity">实体</param>
        /// <param name="tran">数据库操作事务</param>
        /// <returns>自增字段值或影响行数</returns>
        public static long Create(EntityObjectBase objEntity)
        {
            return(Create(objEntity, null));
        }