/// <summary> /// 格式化值项 /// </summary> /// <param name="valueItem"></param> /// <returns></returns> private string FormatValueType(KeyWordInfomation info) { Type valueDataType = null; if (itemValue != null) { valueDataType = itemValue.GetType(); } if (DefaultType.EqualType(valueDataType, DefaultType.StringType) || DefaultType.EqualType(valueDataType, DefaultType.GUIDType)) { return(DataAccessCommon.FormatValue(itemValue, DbType.String, info.DBInfo)); } else if (DefaultType.EqualType(valueDataType, DefaultType.DateTimeType)) { return(DataAccessCommon.FormatValue(itemValue, DbType.DateTime, info.DBInfo)); } else if (DefaultType.EqualType(valueDataType, DefaultType.BytesType)) { return(DataAccessCommon.FormatValue(itemValue, DbType.Binary, info.DBInfo)); } else if (DefaultType.EqualType(valueDataType, DefaultType.BooleanType)) { return(DataAccessCommon.FormatValue(itemValue, DbType.Boolean, info.DBInfo)); } else if (DefaultType.EqualType(valueDataType, DefaultType.GUIDType)) { return(DataAccessCommon.FormatValue(itemValue, DbType.Guid, info.DBInfo)); } else if (valueDataType == null) { return("null"); } return(itemValue.ToString()); }
/// <summary> /// 把该值转成指定类型 /// </summary> /// <param name="value">值</param> /// <param name="type">指定类型</param> /// <returns></returns> private static object ConvertTo(object value, Type type) { if (DefaultType.EqualType(type, DefaultType.BytesType)) { return(StringToBytes(value.ToString())); } return(Buffalo.Kernel.CommonMethods.ChangeType(value, type)); }
/// <summary> /// 把类型转换成字符串 /// </summary> /// <param name="value">类型</param> /// <returns></returns> public static string ValueToString(object value) { if (value == null) { return(""); } else if (DefaultType.EqualType(value.GetType(), DefaultType.BytesType)) { return(ToByteString((byte[])value)); } return(value.ToString()); }
/// <summary> /// 根据Reader结构和实体属性的映射生成空的DataTable /// </summary> /// <param name="reader">Reader</param> /// <param name="datatableName">数据表名</param> /// <param name="entityType">实体类</param> /// <param name="isEmpty">是否生成空的DataTable</param> /// <returns></returns> public static DataTable GenerateDataTable(IDataReader reader, string datatableName, Type entityType, bool isEmpty) { DataTable dt = new DataTable(); EntityInfoHandle entityInfo = EntityInfoManager.GetEntityHandle(entityType); List <EntityPropertyInfo> lstParamNames = GenerateCache(reader, entityInfo); dt.BeginLoadData(); foreach (EntityPropertyInfo info in lstParamNames) { if (info != null) { Type fieldType = info.FieldType; if (DefaultType.EqualType(fieldType, DefaultType.BooleanType)) { fieldType = typeof(bool); } dt.Columns.Add(info.PropertyName, fieldType); } } if (!isEmpty) { while (reader.Read()) { DataRow dr = dt.NewRow(); for (int i = 0; i < lstParamNames.Count; i++) { if (!reader.IsDBNull(i) && lstParamNames[i] != null) { dr[i] = reader[i]; } } dt.Rows.Add(dr); dt.AcceptChanges(); } } dt.EndLoadData(); return(dt); }
/// <summary> /// 获取此值占用的空间 /// </summary> /// <param name="value">值</param> /// <returns></returns> internal static int CurValueSize(object value) { if (value == null) { return(0); } Type type = value.GetType(); if (type.IsValueType) { return(Marshal.SizeOf(type)); } if (DefaultType.EqualType(type, DefaultType.StringType)) //计算字符串的空间 { int size = (value.ToString().Length + 1) * Marshal.SizeOf(typeof(char)); return(size); } if (DefaultType.EqualType(type, DefaultType.BytesType)) //计算字节数组的空间 { int size = ((byte[])value).Length * Marshal.SizeOf(typeof(byte)); return(size); } return(0); }
/// <summary> /// 新的版本值 /// </summary> /// <param name="val">当前值</param> /// <returns></returns> private object NewConcurrencyValue(object val) { Type objType = val.GetType(); if (DefaultType.EqualType(objType, DefaultType.IntType)) { return((int)val + 1); } if (DefaultType.EqualType(objType, DefaultType.DoubleType)) { return((double)val + 1); } if (DefaultType.EqualType(objType, DefaultType.FloatType)) { return((float)val + 1); } if (DefaultType.EqualType(objType, DefaultType.DateTimeType)) { return(DateTime.Now); } if (DefaultType.EqualType(objType, DefaultType.DecimalType)) { return((decimal)val + 1); } if (DefaultType.EqualType(objType, DefaultType.ByteType)) { return((byte)val + 1); } if (DefaultType.EqualType(objType, DefaultType.SbyteType)) { return((sbyte)val + 1); } if (DefaultType.EqualType(objType, DefaultType.ShortType)) { return((short)val + 1); } if (DefaultType.EqualType(objType, DefaultType.LongType)) { return((long)val + 1); } if (DefaultType.EqualType(objType, DefaultType.ULongType)) { return((ulong)val + 1); } if (DefaultType.EqualType(objType, DefaultType.UShortType)) { return((ushort)val + 1); } if (DefaultType.EqualType(objType, DefaultType.UIntType)) { return((uint)val + 1); } if (DefaultType.EqualType(objType, DefaultType.StringType)) { return(CommonMethods.GuidToString(Guid.NewGuid())); } if (DefaultType.EqualType(objType, DefaultType.GUIDType)) { return(Guid.NewGuid()); } return(null); }