通过企业库的方法实现:将主表中的数据保存到历史表中(历史表比主表多一个自动增长列)

#region 历史数据插入
        public int AddDispatchHistoryPlan(CoalProducePlanEntity CoalProducePlan)
        {
            int Result = 0;
            string SqlText = @"Insert SC_DispatchHistoryPlan (ProducePlanID,DeptCode,DeptName,DecPlanQuantity,NovPlanQuantity,OctoberPlanQuantity,SeptemberPlanQuantity,AugustPlanQuantity,JulyPlanQuantity,JunPlanQuantity,MayPlanQuantity,AprilPlanQuantity,MarPlanQuantity,FenPlanQuantity,JanPlanQuantity,FirstQuarterPlan,SecondQuarterPlan,ThridQuarterPlan,FourQuarterPlan,FirstHalfYearPlan,SecondHalfYearPlan,YearPlanQuantity,PlaningType,YearDate,Status,Operator,Auditor,OperationDate,AuditDate,AuditDescription,AuditState,ApplyStatus,Remark,ApplyDescription,InOutStatus) Values (@ProducePlanID,@DeptCode,@DeptName,@DecPlanQuantity,@NovPlanQuantity,@OctoberPlanQuantity,@SeptemberPlanQuantity,@AugustPlanQuantity,@JulyPlanQuantity,@JunPlanQuantity,@MayPlanQuantity,@AprilPlanQuantity,@MarPlanQuantity,@FenPlanQuantity,@JanPlanQuantity,@FirstQuarterPlan,@SecondQuarterPlan,@ThridQuarterPlan,@FourQuarterPlan,@FirstHalfYearPlan,@SecondHalfYearPlan,@YearPlanQuantity,@PlaningType,@YearDate,@Status,@Operator,@Auditor,@OperationDate,@AuditDate,@AuditDescription,@AuditState,@ApplyStatus,@Remark,@ApplyDescription,@InOutStatus) ";
            using (DbCommand Command = DataBase.GetSqlStringCommand(SqlText))
            {
                FillDataParameters(Command, CoalProducePlan);
                Result = DataBase.ExecuteNonQuery(Command);
            }
            return Result;
        }

        private void FillDataParameters(DbCommand command, CoalProducePlanEntity entity)
        {
            System.Reflection.PropertyInfo[] PropertyInfos = typeof(CoalProducePlanEntity).GetProperties();
            foreach (System.Reflection.PropertyInfo PropertyInfo in PropertyInfos)
            {
                object PropertyValue = PropertyInfo.GetValue(entity, null);
                string PropertyName = PropertyInfo.Name;
                Type propertyType = PropertyInfo.PropertyType;
                DbType dataType = GetParameterType(propertyType);
                if ((propertyType == typeof(DateTime)) && (((DateTime)PropertyValue) == DateTime.MinValue))
                    PropertyValue = DBNull.Value;
                DataBase.AddInParameter(command, "@" + PropertyName, dataType, PropertyValue);
            }
        }
        private DbType GetParameterType(Type type)
        {
            if (type == typeof(Int16)) return DbType.Int16;
            if (type == typeof(Int32)) return DbType.Int32;
            if (type == typeof(int)) return DbType.Int32;
            if (type == typeof(string)) return DbType.String;
            if (type == typeof(Guid)) return DbType.Guid;
            if (type == typeof(bool)) return DbType.Boolean;
            if (type == typeof(bool)) return DbType.Boolean;
            if (type == typeof(Double)) return DbType.Double;
            if (type == typeof(double)) return DbType.Double;
            if (type == typeof(float)) return DbType.Double;
            if (type == typeof(decimal)) return DbType.Decimal;
            if (type == typeof(Decimal)) return DbType.Decimal;
            if (type == typeof(short)) return DbType.UInt16;
            if (type == typeof(UInt16)) return DbType.UInt16;
            if (type == typeof(DateTime)) return DbType.DateTime;
            if (type == typeof(byte)) return DbType.Byte;
            if (type == typeof(Byte)) return DbType.Byte;
            if (type == typeof(long)) return DbType.Int64;
            return DbType.String;
        }
        #endregion

posted @ 2013-07-26 10:27  namehwh  阅读(211)  评论(0编辑  收藏  举报