Esempio n. 1
0
 internal static DbType GetEnumDbType(Type enumType)
 {
     if (EnumMetaDataCacheManager.GetEnumStorageType(enumType) == EnumStorageType.EnumValue)
     {
         return(DbType.Int32);
     }
     else
     {
         return(DbType.AnsiString);
     }
 }
Esempio n. 2
0
        private static string GetCondition(DataQueryType dataQueryType, object propertyValue, string columnName, Operator optr)
        {
            if (propertyValue == null)
            {
                return(string.Empty);
            }

            if (optr == Operator.In)
            {
                #region In
                if (propertyValue is IList <long> )
                {
                    return(string.Format("{0} IN ({1})", columnName, GetSqlInConditions((IEnumerable <long>)propertyValue)));
                }
                else if (propertyValue is IList <int> )
                {
                    return(string.Format("{0} IN ({1})", columnName, GetSqlInConditions((IEnumerable <int>)propertyValue)));
                }
                else if (propertyValue is IList <string> )
                {
                    return(string.Format("{0} IN ({1})", columnName, GetSqlInConditions((IEnumerable <string>)propertyValue)));
                }
                else
                {
                    return(string.Empty);
                }
                #endregion
            }

            if (optr == Operator.NotIn)
            {
                #region Not In
                if (propertyValue is IList <long> )
                {
                    return(string.Format("{0} Not IN ({1})", columnName, GetSqlInConditions((IEnumerable <long>)propertyValue)));
                }
                else if (propertyValue is IList <int> )
                {
                    return(string.Format("{0} Not IN ({1})", columnName, GetSqlInConditions((IEnumerable <int>)propertyValue)));
                }
                else if (propertyValue is IList <string> )
                {
                    return(string.Format("{0} Not IN ({1})", columnName, GetSqlInConditions((IEnumerable <string>)propertyValue)));
                }
                else
                {
                    return(string.Empty);
                }
                #endregion
            }

            if (dataQueryType == DataQueryType.String)
            {
                #region
                string value = (string)propertyValue;
                value = value.Replace(@"\", @"\\");
                if (string.IsNullOrEmpty(value))
                {
                    return(string.Empty);
                }
                switch (optr)
                {
                case Operator.Eq:
                    return(string.Format("{0}='{1}'", columnName, value));

                case Operator.NotEq:
                    return(string.Format("{0}!='{1}'", columnName, value));

                case Operator.Like:
                    return(string.Format("{0} like '%{1}%'", columnName, value));

                case Operator.LeftLike:
                    return(string.Format("{0} like '{1}%'", columnName, value));

                case Operator.RightLike:
                    return(string.Format("{0} like '%{1}'", columnName, value));

                default:
                    throw new Exception(string.Format("当属性类型是string时,操作符{0}无效", optr));
                }
                #endregion
            }
            else if (dataQueryType == DataQueryType.DateTime)
            {
                #region
                DateTime value = (DateTime)propertyValue;
                if (value == DateTime.MinValue)
                {
                    return(string.Empty);
                }
                switch (optr)
                {
                case Operator.Eq:
                    return(string.Format("{0}='{1}'", columnName, value.ToString(_DateTimeFormat)));

                case Operator.NotEq:
                    return(string.Format("{0}!='{1}'", columnName, value.ToString(_DateTimeFormat)));

                case Operator.Gt:
                    return(string.Format("{0}>'{1}'", columnName, value.ToString(_DateTimeFormat)));

                case Operator.Ge:
                    return(string.Format("{0}>='{1}'", columnName, value.ToString(_DateTimeFormat)));

                case Operator.Lt:
                    return(string.Format("{0}<'{1}'", columnName, value.ToString(_DateTimeFormat)));

                case Operator.Le:
                    return(string.Format("{0}<='{1}'", columnName, value.ToString(_DateTimeFormat)));

                default:
                    throw new Exception(string.Format("当属性类型是DateTime时,操作符{0}无效", optr));
                }

                #endregion
            }
            else if (dataQueryType == DataQueryType.Number)
            {
                #region
                switch (optr)
                {
                case Operator.Eq:
                    return(string.Format("{0}={1}", columnName, propertyValue.ToString()));

                case Operator.NotEq:
                    return(string.Format("{0}!={1}", columnName, propertyValue.ToString()));

                case Operator.Gt:
                    return(string.Format("{0}>{1}", columnName, propertyValue.ToString()));

                case Operator.Ge:
                    return(string.Format("{0}>={1}", columnName, propertyValue.ToString()));

                case Operator.Lt:
                    return(string.Format("{0}<{1}", columnName, propertyValue.ToString()));

                case Operator.Le:
                    return(string.Format("{0}<={1}", columnName, propertyValue.ToString()));

                default:
                    throw new Exception(string.Format("当属性类型是整数时,操作符{0}无效", optr));
                }

                #endregion
            }
            else if (dataQueryType == DataQueryType.Enum)
            {
                #region
                if (EnumMetaDataCacheManager.GetEnumStorageType(propertyValue.GetType()) == EnumStorageType.EnumName)
                {
                    string value = propertyValue.ToString();
                    switch (optr)
                    {
                    case Operator.Eq:
                        return(string.Format("{0}='{1}'", columnName, value));

                    case Operator.NotEq:
                        return(string.Format("{0}!='{1}'", columnName, value));

                    default:
                        throw new Exception(string.Format("当属性类型是枚举时,操作符{0}无效", optr));
                    }
                }
                else
                {
                    int value = (int)propertyValue;
                    switch (optr)
                    {
                    case Operator.Eq:
                        return(string.Format("{0}={1}", columnName, value));

                    case Operator.NotEq:
                        return(string.Format("{0}!={1}", columnName, value));

                    default:
                        throw new Exception(string.Format("当属性类型是枚举时,操作符{0}无效", optr));
                    }
                }
                #endregion
            }
            else
            {
                throw new Exception(string.Format("查询条件的属性类型{0}无效", dataQueryType));
            }
        }