Esempio n. 1
0
        /// <summary>
        /// 获取该类型的默认值
        /// </summary>
        /// <param name="objType"></param>
        /// <returns></returns>
        public static TypeItem GetDefaultValue(Type objType)
        {
            string   typeName = objType.FullName;
            TypeItem item     = null;

            _dicDefaultValues.TryGetValue(typeName, out item);
            return(item);
        }
Esempio n. 2
0
        /// <summary>
        /// 把C#类型转换成SQL类型
        /// </summary>
        /// <param name="valueType"></param>
        /// <returns></returns>
        public static DbType ToDbType(Type valueType)
        {
            TypeItem item = GetDefaultValue(valueType);

            if (item != null)
            {
                return(item.DbType);
            }
            return(DbType.Object);
        }
Esempio n. 3
0
        /// <summary>
        /// 获取C#类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Type GetCSharpVariableType(DbType type)
        {
            int index = (int)type;

            if (index >= 0 && index < dbTypeMapping.Length)
            {
                TypeItem item = dbTypeMapping[index];
                if (item != null)
                {
                    return(item.ItemType);
                }
            }
            return(null);
        }
Esempio n. 4
0
        /// <summary>
        /// 判断是否默认值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDefaultValue(object value)
        {
            if (value == null)
            {
                return(true);
            }
            string   typeName = value.GetType().FullName;
            TypeItem item     = null;

            if (_dicDefaultValues.TryGetValue(typeName, out item))
            {
                return(value.Equals(item.DefaultValue));
            }
            return(false);
        }
Esempio n. 5
0
        private static Dictionary <string, TypeItem> InitDefauleValues()
        {
            Dictionary <string, TypeItem> ret = new Dictionary <string, TypeItem>();

            dbTypeMapping = new TypeItem[28];


            TypeItem item;


            item = new TypeItem(typeof(DateTime), DefaultValue.DefaultDateTime, DbType.DateTime);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;
            dbTypeMapping[(int)DbType.Date] = item;
            dbTypeMapping[(int)DbType.Time] = item;
            dbTypeMapping[26] = item; //DateTime2
            dbTypeMapping[27] = item; //DateTimeOffset



            item = new TypeItem(typeof(byte[]), null, DbType.Binary);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;



            item = new TypeItem(typeof(System.DBNull), System.DBNull.Value, DbType.Object);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;



            item = new TypeItem(typeof(Guid), DefaultValue.DefaultGuid, DbType.Guid);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;


            item = new TypeItem(typeof(int), DefaultValue.DefaultInt, DbType.Int32);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;


            item = new TypeItem(typeof(double), DefaultValue.DefaultDouble, DbType.Double);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;


            item = new TypeItem(typeof(float), DefaultValue.DefaultFloat, DbType.Single);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;


            item = new TypeItem(typeof(decimal), DefaultValue.DefaultDecimal, DbType.Decimal);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType]       = item;
            dbTypeMapping[(int)DbType.Currency]   = item;
            dbTypeMapping[(int)DbType.VarNumeric] = item;

            item = new TypeItem(typeof(sbyte), DefaultValue.DefaultSbyte, DbType.SByte);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;

            item = new TypeItem(typeof(byte), DefaultValue.DefaultSbyte, DbType.Byte);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;

            item = new TypeItem(typeof(short), DefaultValue.DefaultShort, DbType.Int16);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;


            item = new TypeItem(typeof(long), DefaultValue.DefaultLong, DbType.Int64);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;

            item = new TypeItem(typeof(string), null, DbType.String);
            ret.Add(item.ItemType.FullName, item);
            dbTypeMapping[(int)item.DbType] = item;
            dbTypeMapping[(int)DbType.StringFixedLength]     = item;
            dbTypeMapping[(int)DbType.AnsiString]            = item;
            dbTypeMapping[(int)DbType.AnsiStringFixedLength] = item;

            return(ret);
        }