示例#1
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);
            }
        }
        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);
        }