数据库中的返回值和真实类型值的转换
2014-11-14 10:20:40 访问(1979) 赞(0) 踩(0)
在我本人的开发习惯中:
1、bool值数据库中用char(1)存储,0=false,1=true
2、枚举值在数据库中用int存储
因此,转义主要是对bool和Enum类型进行有效的转义
/// <summary>
///
/// </summary>
public readonly static Type typeBoolean = typeof(Boolean);
/// <summary>
///
/// </summary>
public readonly static Type typeDateTime = typeof(DateTime);
/// <summary>
///
/// </summary>
public readonly static Type typelong = typeof(long);
/// <summary>
///
/// </summary>
public readonly static Type typeint = typeof(int);
/// <summary>
///
/// </summary>
public readonly static Type typestring = typeof(string);
/// <summary>
///
/// </summary>
public readonly static Type typeEnum = typeof(Enum);
/// <summary>
/// 转义枚举
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public static object ConvertEnumToValue(object theValue)
{
System.Enum em = (System.Enum)theValue;
IConvertible ic = (IConvertible)em;
if (ic == null)
new Exception("未知枚举值!");
try
{
return ic.ToInt64(null);
}
catch (Exception err)
{
throw err;
}
}
/// <summary>
///
/// </summary>
/// <param name="valueType"></param>
/// <param name="theValue"></param>
/// <returns></returns>
public static object GetObjectValue(Type valueType, object theValue)
{
if (theValue == null)
return null;
if (valueType == null)
return theValue;
if (valueType == typeEnum || valueType.BaseType == typeEnum)
return ConvertEnumToValue(theValue);
else
return theValue;
}
/// <summary>
/// 转义变量值
/// </summary>
/// <param name="valueType"></param>
/// <param name="theValue"></param>
/// <returns></returns>
public static object ConvertObject(Type valueType, object theValue)
{
if (theValue == null)
return null;
if (valueType == typestring)
{
if (theValue == null)
return null;
else
return theValue.ToString();
}
if (valueType == typeBoolean)
{
return GetBooleanNullByObj(theValue);
}
if (valueType == typelong)
{
return Convert.ToInt64(theValue);
}
if (valueType == typeint)
{
return Convert.ToInt32(theValue);
}
if (valueType.BaseType == typeEnum)
{
return Enum.Parse(valueType, theValue.ToString());
}
return theValue;
}
标签:
数据库中的返回值和真实类型值的转换 


上一条:
下一条:
相关评论
发表评论