Пример #1
0
        /// <summary>
        /// 条件
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="t"></param>
        /// <param name="OperType">操作类型</param>
        /// <param name="Exp">条件</param>
        /// <param name="Sort">排序</param>
        /// <returns></returns>
        public GDModel.MergerModel GDCodition <T>(T t, SQLEnum.OperationType OperType, Expression <Func <T, bool> > Exp = null) where T : new()
        {
            PropertyInfo[] propertys = t.GetType().GetProperties();
            string         SQLStr    = string.Empty;
            string         Where     = (Exp == null ? "" : Exp.DealExpress());
            string         TableName = typeof(T).Name;

            if (OperType == SQLEnum.OperationType.Add || OperType == SQLEnum.OperationType.Update)
            {
                List <string> FileName = new List <string>();
                List <string> Value    = new List <string>();
                string        DbValue  = string.Empty;
                foreach (PropertyInfo pi in propertys)
                {
                    GDColoum objAttrs = pi.GetCustomAttribute <GDColoum>();
                    if (objAttrs != null && !objAttrs.IsKey)
                    {
                        object obj = pi.GetValue(t, null);
                        if (obj != null)
                        {
                            if (OperType == SQLEnum.OperationType.Add)
                            {
                                FileName.Add(pi.Name);
                                Value.Add(pi.GetValue(t, null).ToValStr());
                            }
                            else
                            {
                                FileName.Add($"{pi.Name}={pi.GetValue(t, null).ToValStr()}");
                            }
                        }
                    }
                }
                if (OperType == SQLEnum.OperationType.Add)
                {
                    SQLStr = $"INSERT INTO {TableName}({string.Join(",", FileName)}) values({string.Join(",", Value)})";
                }
                else
                {
                    SQLStr = $"UPDATE {TableName} set {string.Join(",", FileName)} where 1=1" + (string.IsNullOrEmpty(Where) ? "" : " and " + Where);
                }
            }
            else
            {
                SQLStr = $"DELETE FROM {TableName} where 1=1 {Where}";
            }
            return(new GDModel.MergerModel()
            {
                SQLStr = SQLStr, Entity = t
            });
        }
Пример #2
0
        /// <summary>
        /// 条件
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="t"></param>
        /// <param name="OperType">操作类型</param>
        /// <param name="Exp">条件</param>
        /// <param name="Sort">排序</param>
        /// <returns></returns>
        public static GDModel.MergerModel GDCodition <T>(this T t, SQLEnum.OperationType OperType, Expression <Func <T, bool> > Exp = null) where T : new()
        {
            SQLServerBSHelper Bs = new SQLServerBSHelper(ConnectName);

            return(Bs.GDCodition(t, OperType, Exp));
        }